Did you happen to notice in that post that in fact only 4 objects are being stored in the file: one ArrayList and three Badarians? Okay, the ArrayList holds an Object which is a fifth object and each Badarian contains a String
(the other private instance Objects are null), but that still only pushes it to 8 objects in total.
Also, your comment states "401 bytes file generated." I ran this same code (outside of a servlet
) on my machine (WinXP) and it generated a file of 500,401 bytes -- slightly less than 500kb, off by a factor of 1,000.
The reason is that Serialization will store each instance only once, even if it is referenced eight billion times. If you had 100,000 unique instances, the file size would be much bigger. Looking at the generated file shows that it was able to store each duplicated reference as 5 bytes. This makes since given 100,000 references comes out to 500,401 bytes, leaving 401 bytes for the 3 Badarians and the ArrayList.
Another note, if you are going to create an ArrayList of a known size, you can presize it ahead of time. While the difference was slight on my machine (Athlon XP 2800+, ran in about 550ms vs. 650ms, too short for a reliable benchmark), it may be more pronounced if you're seeing 8 second runtimes. Just pass in a parameter to the constructor specifying how many slots it should have to start: new ArrayList(100002). Otherwise, every time it fills up it needs to reallocate and copy the internal array.
Try having your code create 100,000 random Badarian instances to get a better feel for performance and disk space needed. Also, don't include creating the random instances in the time since you won't normally be doing that.
[ February 14, 2005: Message edited by: David Harkness ]