I am using a HashMap to cache some important data when my application starts.
This cache sometime grows and then reduced. I want to know how much memory it is taking on RAM.
Is it possible to get memory taken by a map in KB? Is there any API?
Only the reference to the list is stored in the value. In a 32 bits JVM, in one Map entry, you have 4 bytes for the "aby" reference + 4 bytes for the List reference + 4 bytes for the "hashcode" int property of Map entry + 4 bytes for the "next" property of Map entry.
The map size is always constant. i.e., 1 . But default size of hash map is 16 bits .
Map size() method in Java is used to get the total number entries i.e, key-value pair. So this method is useful when you want total entries present on the map. If the map contains more than Integer. MAX_VALUE elements return Integer.
i'm using this method to estimate the MAP size:
public static void size(Map map) {
try{
System.out.println("Index Size: " + map.size());
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeObject(map);
oos.close();
System.out.println("Data Size: " + baos.size());
}catch(IOException e){
e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With