假设我有一个类进行一些繁重的处理,使用多个集合进行操作。我想要做的是确保这样的操作不会导致内存不足,甚至更好的是我想设置它可以使用多少内存的阈值。
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?
}
}
这样做的正确方法是什么?或者这是不可能/不可行的?