Java 单元测试:如何测量方法调用的内存占用 [英] Java unit testing: how to measure memory footprint for method call

查看:33
本文介绍了Java 单元测试:如何测量方法调用的内存占用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类进行一些繁重的处理,操作多个集合.我想要做的是确保这样的操作不会导致内存不足,或者更好的是我想设置一个它可以使用多少内存的阈值.

Assuming I have a class that does some heavy processing, operating with several collections. What I want to do is to make sure that such operation can't lead to out-of-memory or even better I want to set a threshold of how much memory it can use.

class MyClass()
{
   public void myMethod()
   {
      for(int i=0; i<10000000; i++)
      {
         // Allocate some memory, may be several collections
      }
   }
}

class MyClassTest
{
   @Test
   public void myMethod_makeSureMemoryFootprintIsNotBiggerThanMax()
   {
      new MyClass().myMethod(); 
      // How do I measure amount of memory it may try to allocate?
   }
}

这样做的正确方法是什么?或者这是不可能的/不可行的?

What is the right approach to do this? Or this is not possible/not feasible?

推荐答案

我能想到几个方案:

  • Finding out how much memory your method requires via a microbenchmark (i.e. jmh).
  • Building allocation strategies based on heuristic estimation. There are several open source solutions implementing class size estimation i.e. ClassSize. A much easier way could be utilizing a cache which frees rarely used objects (i.e. Guava's Cache). As mentioned by @EnnoShioji, Guava's cache has memory-based eviction policies.

您还可以编写自己的基准测试来计算内存.这个想法是

You can also write your own benchmark test which counts memory. The idea is to

  1. 运行一个线程.
  2. 创建一个新数组来存储要分配的对象.所以这些对象不会在 GC 运行期间被收集.
  3. System.gc(), memoryBefore = runtime.totalMemory() - runtime.freeMemory()
  4. 分配对象.将它们放入数组中.
  5. System.gc(), memoryAfter = runtime.totalMemory() - runtime.freeMemory()

这是我在我的轻量级微基准测试工具,能够以字节精度测量内存分配.

This is a technique I used in my lightweight micro-benchmark tool which is capable of measuring memory allocation with byte-precision.

这篇关于Java 单元测试:如何测量方法调用的内存占用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆