Java内存基准

时间:2014-03-10 14:11:51

标签: java memory data-structures benchmarking microbenchmark

我目前正在开发自己的Java宠物项目,它包含自定义数据结构。 为了衡量性能,我选择了Google Caliper框架,但为了测量数据结构的内存使用情况,我应该每次使用VisuamVm(转储堆并等待对象保留大小的计算)来测量它,以获得有效的结果。我想做一些“内存基准”测试。

所以,问题是:

是否有类似于CaliperJunitBenchmarks的Java框架允许进行测试以测量数据结构的内存消耗?

3 个答案:

答案 0 :(得分:2)

此工具适合您。 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

答案 1 :(得分:0)

虽然我不知道有哪个库,但有更好的方法比使用堆转储来节省内存:您可以要求JVM使用System.gc()执行garbace集合,然后查询堆使用Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()

答案 2 :(得分:0)

内存基准测试包含两点:吞吐量片段。在这里,我认为您想要做的是为这些环境选择更好的数据结构。对于这一点,我认为你还需要这两点来衡量。

在这里,您可以使用 OQL (对象查询语言)来查询此数据结构用法。您可以使用 hprof MAT 来分析系统的堆上下文。

为了解决测量内存消耗的问题,我建议采取以下步骤:

  1. 创建高效的环境
  2. 运行程序
  3. 重要的是,您可以转储堆Use JMX to make HPROF heap dumps right before and after a full GC
  4. 使用 MAT VisualVM 分析hprof