假设我有一个进行一些重度处理的类,操作多个集合。我想要做的是确保这种操作不会导致内存溢出,或者更好的是,我想设置一个内存使用量的阈值。
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?
}
}
哪种方法是正确的?或者这不可能/不可行吗?