Calculate card input memory

I have a map (String, String), and I want to find the size of memory for the record and the map as a whole. I read somewhere that Instrumentation can be useful ( Instrumentation ). Anyone have an idea?

Thanks in advance.

+2
java dictionary
Dec 08 '13 at 20:11
source share
2 answers

An empty java.util.AbstractMap.SimpleEntry instance should be 24 bytes for a 64-bit JVM and 12 bytes for a 32-bit JVM. Here is the @PeterLawrey technique that I found useful, based on a MemoryUsageExamplesTest :

 System.out.printf("The average memory used by simple entry is %.1f bytes%n", new SizeofUtil() { @Override protected int create() { Map.Entry<String, String> e = new AbstractMap.SimpleEntry<String, String>(null, null); return 1; } }.averageBytes()); 
+1
Dec 08
source share

Use ObjectOutputStream to write an object to ByteArrayOutputstream and check the length of the resulting byte array.

Obviously, your Map.Entry will need to implement Serializable.

 public int getSizeInBytes(Serializable someObject) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream objectOut = new ObjectOutputStream(byteOut); objectOut.writeObject(someObject); objectOut.flush(); return byteOut.toByteArray().length; } public int getSizeBits(Serializable someObject) { return getSizeInBytes(someObject) * 8; } 
+1
Dec 08 '13 at 20:46
source share



All Articles