为什么空Java程序消耗内存? [英] Why does an empty Java program consume memory?
问题描述
我正在探索Java中的内存使用情况,以了解我的程序泄漏内存的原因。在我的main while循环中剥离代码之后,我仍然会随着时间的推移增加内存使用量。思考空程序的内存使用情况:
I'm exploring memory usage in Java to understand why my program leaks memory. After stripping off code in my main while loop, I still get an increase of memory usage over time. Pondering the memory usage of an empty program:
class Nothing
{ public static void main(String[] args)
{ while(true); }
}
我仍看到内存增加:
所以我的问题是:为什么还有锯齿形?为什么当GC运行时它没有保存所有内存(每次gc运行(谷值)使用的内存增加10-20Kb(与之前的谷值相比))?
So my question is: Why is there still a saw tooth pattern? Why when the GC runs does it not save all the memory (each time the gc runs (the valleys) the used memory increases by 10-20Kb (compared to the previous valley))?
编辑:
java版1.6.0_29
java version "1.6.0_29"
Java(TM)SE运行时环境(版本1.6.0_29-b11)
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM)客户端VM(版本20.4-b02,混合模式,共享)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)
OS:Windows 7 Enterprise-32位
OS: Windows 7 Enterprise-32 bit
推荐答案
为什么还有锯齿形?
Why is there still a saw tooth pattern?
如果我没弄错的话,部分原因是监视器本身正在强制应用程序创建临时对象,其中包含有关垃圾收集和内存使用状态的信息。
If I'm not mistaken, part of the reason for this is that the monitor itself is forcing the application to create temporary objects that contain information about the state of garbage-collection and memory usage.
为什么当GC运行不会保存所有内存(每次gc运行(谷值)使用的内存增加10-20Kb(与previ相比) ous valley))?
Why when the GC runs does it not save all the memory (each time the gc runs (the valleys) the used memory increases by 10-20Kb (compared to the previous valley))?
我相信显示器在垃圾收集后不会立即看到已用内存的数量,而是必须经常轮询以查看内存使用情况。因此,感知的山谷必然略高于真正的山谷。我会猜测,谷底内存使用量的感知增加只是这种差异的随机误差,并且随着时间的推移会被中和。 (也就是说,我不认为每个周期实际上有10-20 KB的内存泄漏。)
I believe the monitor does not instantly see the amount of used memory after garbage-collection, but rather, must poll frequently to see memory usage. Therefore, the perceived "valley" is necessarily somewhat higher than the true valley. I would guess that the perceived increase in memory usage at the valleys is just a random artifact of this discrepancy, and would be neutralized over time. (That is, I don't think that there's actually 10-20 KB of memory leak occurring in each cycle.)
这篇关于为什么空Java程序消耗内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!