为什么一个空的 Java 程序会消耗内存? [英] Why does an empty Java program consume memory?

查看:17
本文介绍了为什么一个空的 Java 程序会消耗内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索 Java 中的内存使用情况,以了解为什么我的程序会泄漏内存.在我的主 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)

操作系统: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(与之前的谷值相比))?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆