我创建了一个函数来测试GC并查看内存使用情况。结果很奇怪。
内存分析类:
public class MemoryAnalizer {
private static NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
private static DecimalFormat df = (DecimalFormat) nf;
static long heapSize;
static long heapMaxSize;
static long heapFreeSize;
static long used;
public static void analize()
{
heapSize = Runtime.getRuntime().totalMemory();
heapMaxSize = Runtime.getRuntime().maxMemory();
heapFreeSize = Runtime.getRuntime().freeMemory();
used = heapSize - heapFreeSize;
System.out.println("MemoryAnalitics: [ Used: " + df.format(used / 1000) + " Free: " + df.format(heapFreeSize / 1000) + " HeapSize: " + df.format(heapSize / 1000) + " | Max HeapSize: " + df.format(heapMaxSize / 1000) + " (KBs)]");
}
}
在GC调用之前和之后调用内存分析的Main函数:
private static MemoryAnalizer memory = new MemoryAnalizer();
public static void main(String argv[])
{
while (true)
{
System.out.println();
memory.analize();
System.gc();
memory.analize();
}
}
生成的打印件显示以下结果:
MemoryAnalitics: [ Used: 977 Free: 124.852 HeapSize: 125.829 | Max HeapSize: 1.860.698 (KBs)]
MemoryAnalitics: [ Used: 305 Free: 125.523 HeapSize: 125.829 | Max HeapSize: 1.860.698 (KBs)]
MemoryAnalitics: [ Used: 977 Free: 124.852 HeapSize: 125.829 | Max HeapSize: 1.860.698 (KBs)]
MemoryAnalitics: [ Used: 305 Free: 125.523 HeapSize: 125.829 | Max HeapSize: 1.860.698 (KBs)]
MemoryAnalitics: [ Used: 977 Free: 124.852 HeapSize: 125.829 | Max HeapSize: 1.860.698 (KBs)]
MemoryAnalitics: [ Used: 305 Free: 125.523 HeapSize: 125.829 | Max HeapSize: 1.860.698 (KBs)]
什么可以解释每个while循环之间增加600 KB的内存?