在执行期间,java程序如何告诉它使用了多少内存? [英] During execution, how can a java program tell how much memory it is using?

查看:84
本文介绍了在执行期间,java程序如何告诉它使用了多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行期间,java程序如何判断它正在使用多少内存?

During execution, how can a java program tell how much memory it is using?

我不在乎它的效率如何!

I don't care how efficient it is!

推荐答案

VonC的答案是一个交互式解决方案 - 如果你想以编程方式知道,你可以使用 Runtime.totalMemory()以查找JVM使用的总量,并且 Runtime.freeMemory()来查找有多少仍然可用(即它被分配 JVM,但没有在 JVM中分配 - 新对象可以使用这个内存)。

VonC's answer is an interactive solution - if you want to know programatically, you can use Runtime.totalMemory() to find out the total amount used by the JVM, and Runtime.freeMemory() to find out how much of that is still available (i.e. it's allocated to the JVM, but not allocated within the JVM - new objects can use this memory).

这些是实例方法 - 使用 Runtime.getRuntime()首先获取单例实例。

These are instance methods - use Runtime.getRuntime() to first get the singleton instance.

这篇关于在执行期间,java程序如何告诉它使用了多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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