The moose likes Java in General and the fly likes How to tell what is the size of an object (Map) in terms of kilo bytes. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to tell what is the size of an object (Map) in terms of kilo bytes." Watch "How to tell what is the size of an object (Map) in terms of kilo bytes." New topic
Author

How to tell what is the size of an object (Map) in terms of kilo bytes.

Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
Hi all,

I have a hashMap<String, Object> and wonder how can I tell what is the size of it (in kilo bytes) meaning, if I wrote it to a file, what will be the size of the file.

thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

That would depend on how you wrote it to the file. You could write it in Properties format or as XML or using Java serialization, or an infinite number of other ways. All of these are going to produce files of different sizes.

Do you have a use case where you need to know this number in advance of actually creating the file?
Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
I think that one of the map in my application stores a lot of data and it 'eats' my memory. I wonder if there's a way to tell the size (kilo bytes) of the map so I can asses which one is growing in memory.
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
Then I'm sure it's not the map itself that "eats" the memory, but the objects stored in it. I'd approximately count like this (in bytes):
4 * map.size() + 2 * sum of all key (String) lengths + sum of all values. Of course the most problematic question now is "What is the size of your value object?". If it keeps references to many other objects, then the memory consumption can be high.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to tell what is the size of an object (Map) in terms of kilo bytes.
 
Similar Threads
WA #1.....word association
B&S: Data File Format
copy error
Bits or Bytes
Is it possible to truncate some data from end of file?