Java 单元测试:如何测量方法调用的内存占用 [英] Java unit testing: how to measure memory footprint for method call
问题描述
假设我有一个类进行一些繁重的处理,操作多个集合.我想要做的是确保这样的操作不会导致内存不足,或者更好的是我想设置一个它可以使用多少内存的阈值.
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?
推荐答案
我能想到几个方案:
- 通过微基准测试(即jmh).
- 基于启发式估计构建分配策略.有几种实现类大小估计的开源解决方案,即 ClassSize.一种更简单的方法是利用缓存来释放很少使用的对象(即 Guava 的缓存).正如@EnnoShioji 所提到的,Guava 的缓存具有基于内存的驱逐策略.
- 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
- 运行一个线程.
- 创建一个新数组来存储要分配的对象.所以这些对象不会在 GC 运行期间被收集.
System.gc()
,memoryBefore = runtime.totalMemory() - runtime.freeMemory()
- 分配对象.将它们放入数组中.
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屋!