我使用1.6.0_27-b07(Sun的JRE)的默认GC,因为this我无法使用Runtime.getRuntime()。freeMemory()检测到内存增加。任何人都可以阐明如何实现这一目标吗?我是否必须使用其他GC?哪一个?
下面的简单程序为分配的内存打印0。 :( :( :(
import java.util.HashSet;
import java.util.Set;
public class MemoryUtils {
private static Set<String> set = new HashSet<String>();
public static void main(String[] args) {
long mem = Runtime.getRuntime().freeMemory();
// now create a bunch of objects and save them in the set
for (int i = 0; i < 100; i++) {
set.add("foo" + i);
}
long allocated = mem - Runtime.getRuntime().freeMemory();
System.out.println("Memory allocated: " + allocated); // ===> GIVES 0 !!!!
}
}
答案 0 :(得分:3)
这些对象将在为短期对象保留的空间中分配,并且只有在它们存活一段时间后才会移动到主堆。
如果您想查看应用程序创建的所有对象的确切情况,您确实需要使用分析器。
以下是一些信息:Java Memory explained (SUN JVM)