سؤال

I'm currently developing own pet project in Java which consist of custom data structures. To measure performance I chose Google Caliper framework, but to measure memory usage of data structure I should each time measure it using VisuamVm (dump heap and wait for calculation of object retained size), to get valid results. I want to make some kind of "memory benchmark" tests.

So, the question is:

Is there any Java framework similar to Caliper or JunitBenchmarks that allows to make tests to measure memory consumption of my data structures?

هل كانت مفيدة؟

المحلول

This tool is appropriate for you. Open JDK - Java Object Layout

Sample examining HashMap

$ java -jar jol-cli/target/jol-internals.jar java.util.HashMap
  Running 64-bit HotSpot VM.
  Using compressed references with 3-bit shift.
  Objects are 8 bytes aligned.
  Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
  Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

  java.util.HashMap object internals:
   OFFSET  SIZE       TYPE DESCRIPTION                    VALUE
    0     4            (object header)                01 00 00 00 (00000001 00000000    00000000 00000000)
    4     4            (object header)                00 00 00 00 (00000000 00000000 00000000 00000000)
    8     4            (object header)                0f 0f 3e e0 (00001111 00001111 00111110 11100000)
   12     4        Set AbstractMap.keySet             null
   16     4 Collection AbstractMap.values             null
   20     4        int HashMap.size                   0
   24     4        int HashMap.threshold              16
   28     4      float HashMap.loadFactor             0.75
   32     4        int HashMap.modCount               0
   36     4        int HashMap.hashSeed               0
   40     4    Entry[] HashMap.table                  []
   44     4        Set HashMap.entrySet               null
   Instance size: 48 bytes (estimated, add this JAR via -javaagent: to get accurate result)
   Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

نصائح أخرى

While I don't know of a library for that, there are better approaches than taking a heap dump to mesaure memory use: You can ask the JVM to perform a garbace collection with System.gc(), and then query heap usage with Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() .

Memory benchmark contains two points: throughput and fragment. Here I think what you want to do is choose what's the better data structure for these environment. For this point, I think you also need these two points to measure.

Here you can use OQL(Object query language) to query this datastructure usage. You can use hprof and MAT to analysis your system's heap context.

To solve your the question that making tests to measure the memory consumption, I suggest these steps:

  1. Create a productive environment
  2. Run the program
  3. In the important point, you can dump your heap Use JMX to make HPROF heap dumps right before and after a full GC
  4. Use MAT, VisualVM to analysis the hprof
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top