利用Runtime类检查程序内存占用情况

Runtime类封装了运行时的环境。每个Java应用程序都有一个Runtime 类实例,使应用程序能够与其运行的环境相连接。

一般不能实例化一个Runtime对象,应用程序也不能创建自己的Runtime 类实例,但可以通过getRuntime 方法获取当前Runtime运行时对象的引用。

一旦得到了一个当前的Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为。

代码如下:
Runtime currRuntime = Runtime.getRuntime ();
int nFreeMemory = ( int ) (currRuntime.freeMemory() / 1024 / 1024);
int nTotalMemory = ( int ) (currRuntime.totalMemory() / 1024 / 1024);
System.out.println(" 内存信息 :" + nFreeMemory + "M/" + nTotalMemory + "M(free/total)");

你可能感兴趣的:(利用Runtime类检查程序内存占用情况)