*
The moose likes I/O and Streams and the fly likes readObject() eat memory !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "readObject() eat memory !!!" Watch "readObject() eat memory !!!" New topic
Author

readObject() eat memory !!!

Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
Hello i have problem when i read object from file that generated from this code :


this is how i read the object, i'm using thread because i want to keep up to date with the file, so it will keep reading in interval... the file that i read is about 15mb... but i dunt know how when i read object it will consume about 100-150mb memory in heap. i'm using jprofiler to measure.

this is code how i read this file :


This is main program :


does anyone know, why my program consumes about 100-150mb when deserialize object from 15mb file.

thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, you've got about 45 MB worth of objects there (1 million times about 20 bytes for an Integer and about 24 for a Map.Entry), plus there's the Hashtable that the ObjectXStreams will use to keep track of whether or not they're already serialized an object, which will consume another ~24M or so; so it looks like either the read or write program will not quite fit into the default 64M heap. I'd think that either one would run within a 100M or so, and not need any more than that.That's the Java heap itself, mind you; the JVM's native code and such consume additional space outside of the heap.


[Jess in Action][AskingGoodQuestions]
Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
thank you for the reply before. but how do you know Integer object cost 20byte and MapEntry 24byte. do you have any reference site, so i can learn how the calculation size.



thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There's no official documentation for that, and it of course varies across JVMs. But the rule I always use, and it works well for me, is to allow 16 bytes for an object, plus the size of its member variables. An Integer has one "int" member, so it's 20 bytes. A Map.Entry has at least two reference variable entries to point to the key and value, so I said 24 bytes. But now that I think of it, it probably has at least one more member so that entries can be in a linked list...

OK, I went and looked at the JDK source code. A Hashtable.Entry has four members: an int to hold a hash value, and three reference variables, so I would count that as 16 + 4 + 12 = 32 bytes; my earlier estimate was a little low, then.
Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
wow, that's really explain much ! thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: readObject() eat memory !!!
 
Similar Threads
Reading hashmap from file
Query
paritial serialization
java.io.StreamCorruptedException: invalid stream header:i understand its a thread issue not sure fix
Behaviour of static variables