• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 26945
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic