Calculate memory of a Map Entry

后端 未结 2 1185
花落未央
花落未央 2021-01-14 21:09

I have a Map(String, String) and I want to find the memory size of an Entry and the Map in total. I read somewhere that Instrumentation might be useful (Instrumentation). Do

2条回答
  •  我在风中等你
    2021-01-14 22:07

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

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

提交回复
热议问题