如何在java中监控空闲内存(包括缓冲区和缓存)?
我已经搜索了可能的解决方案,例如 如何用Java监控计算机的cpu、内存和磁盘使用情况?。但是在缓冲区和缓存中花费的内存没有在任何地方提到?
例如,在 Linux 上,free -m 的输出:
total used free shared buffers cached Mem: 2011 1835 175 0 309 847 -/+ buffers/cache: 678 1333 Swap: 1998 0 1998
我已经测试了 sigar 的输出,报告可用内存为 175 MB。
这个问题的原因是如何检测操作系统何时剩余内存所剩无几?在之前的 free -m
输出中,175 MB 看起来相当低;但通过添加缓冲区和高速缓存,很明显可用内存实际上为 2 GB 中的 1333 MB。
总而言之,使用 java,是否可以获得 1333 MB 的值作为可用内存的值?正如我已经提到的,到目前为止我只知道如何获得 175 MB 的值。
I've already searched for possible solutions, such as How to monitor the computer's cpu, memory, and disk usage in Java?. But memory spent in buffers and cache is not mentioned anywhere?
For example, on linux, output of free -m:
total used free shared buffers cached Mem: 2011 1835 175 0 309 847 -/+ buffers/cache: 678 1333 Swap: 1998 0 1998
I've already tested output of sigar, which reports free memory as 175 MB.
The cause of this question is how to detect when OS has little memory left? In the previous output of free -m
, 175 MB seems quite low; but by adding buffers and cache it is evident that free memory is actually 1333 MB out of 2 GB.
To summarise, using java, is it possible to get value 1333 MB as value of the free memory? As I've already mentioned, so far I know only how to get value of 175 MB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试获取操作系统本身(而不是 JVM)的内存统计信息,可以使用 JNA 或 JNI 直接对操作系统进行特定于平台的本机 API 调用。
If you are trying to get memory statistics for the OS itself (not the JVM), you can use JNA or JNI to make the platform-specific native API calls directly to the OS.
我不直接了解可用内存,但 JVM 维护着 JMX beans,我相信这些 bean 具有以下信息和更多信息: http://docs.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html
I don't know about free memory directly, but the JVM maintains JMX beans that, I believe, have this information and more: http://docs.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html
我确信不可能使用 Java API 来获取 cahce/buffers 数量。即使来自 SIGAR 的人也使用 JNI 包装器和本机系统调用......
I am convinced it is not possible to use Java API for obtaining cahce/buffers number. Even the guys from SIGAR use JNI wrappers and native system calls...