• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22839
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!