Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Size of an arraylist in bytes Date: Sun, 20 Nov 2011 17:19:22 -0500 Organization: A noiseless patient Spider Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 20 Nov 2011 22:19:23 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="HSlJAUb3pGXi3i7ZL/HoAw"; logging-data="27200"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jIndcQSBq+rDbVJXxt0CQ" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: Cancel-Lock: sha1:XFX4iCxyZ5lJ0+yflPUH0mzOJ8c= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10124 On 11/20/2011 4:35 PM, sara wrote: >[...] > But do you have any answer to my second question? Only that you're going about it wrong. As Andreas Leitgeb points out, serializing an object is a different proposition than serializing a bunch of "raw" values: It saves enough information to reconstruct an "image" of the original object, with the same structure. What do I mean by "structure?" Something like this: Integer x = new Integer(42); Integer y = new Integer(42); Here we have two distinct Integer instances, each with the value 42. ArrayList one = new ArrayList(); one.add(x); one.add(x); The first ArrayList holds one of the Integer instances, twice, and has nothing to do with the other. ArrayList two = new ArrayList(); two.add(x); two.add(y); The second ArrayList holds both Integer instances, once each. If you serialize `one' and read it back again, you'll get an ArrayList with two references to the same Integer. Reading it back will produce one Integer, not two. There will be two objects in the serialized stream: One ArrayList and one Integer, plus enough additional information to reassemble them. (Actually, there will probably be additional objects: The ArrayList owns an array, which is an object in its own right, and perhaps there might be others. But there'll be two "visible" objects in the stream.) If you serialize `two' and read it back, you'll get an ArrayList with two references to two distinct Integers: Three "visible" objects in all. It's all right to serialize an object graph and store it on disk. It is *not* all right to try to update the serialization in place, nor to modify the object and expect a re-serialization to have the same size. If you need in-place operations or same-size guarantees, you'll need to invent a different external representation for your data. -- Eric Sosman esosman@ieee-dot-org.invalid