简体   繁体   English

Java 程序如何跟踪其运行期间使用的最大实际堆大小?

[英]How can a Java program keep track of the maximum actual heap size used during its run?

I'm running a certain Java program, with its -Xmx higher than -Xms , ie its heap can grow.我正在运行某个 Java 程序,它的-Xmx高于-Xms ,即它的堆可以增长。 The heap size at execution end is (IIRC) not the maximum used during the run.执行结束时的堆大小是 (IIRC) 不是运行期间使用的最大值。

  • How can I get the current heap size?如何获得当前的堆大小?
  • How can I get the maximum heap size over the course of the run, other than periodically polling the current size "myself"?除了定期轮询“我自己”的当前大小之外,如何在运行过程中获得最大堆大小?

Get current heap size:获取当前堆大小:

public static long getHeapSize(){
    int mb = 1024*1024;

    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();

    return ((runtime.totalMemory() - runtime.freeMemory()) / mb);
}

If you want to inspect it from your program itself, use the methods of the Runtime class:如果要从程序本身检查它,请使用Runtime类的方法:

Runtime.getRuntime().freeMemory()
Runtime.getRuntime().maxMemory()

If it is ok for you to inspect the heap size from outside your program, you should use JVisualVM.如果您可以从程序外部检查堆大小,则应该使用 JVisualVM。 You can find it in the bin folder of your JDK.您可以在 JDK 的 bin 文件夹中找到它。 Simply start it, and attach to your java program to get insight into your programs heap usage.只需启动它,并附加到您的 Java 程序以深入了解您的程序堆使用情况。 It will display a graph of your heap usage, making it easy to find the maximum heap size of the run of your program.它将显示您的堆使用情况图,从而可以轻松找到程序运行的最大堆大小。

// Get current size of heap in bytes // 获取当前堆的大小(以字节为单位)

long heapSize = Runtime.getRuntime().totalMemory(); 

// Get maximum size of heap in bytes. // 以字节为单位获取堆的最大大小。 The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.堆不能超过这个大小。// 任何尝试都将导致 OutOfMemoryException。

long heapMaxSize = Runtime.getRuntime().maxMemory();

// Get amount of free memory within the heap in bytes. // 以字节为单位获取堆内的空闲内存量。 This size will increase // after garbage collection and decrease as new objects are created. // 在垃圾收集之后,这个大小会增加,并随着新对象的创建而减少。

long heapFreeSize = Runtime.getRuntime().freeMemory(); 

The other answers provide the mechanics of doing via Runtime.getRuntime().totalMemory() and maxMemory(), but be VERY careful - the answer given will be true at a given instant only.其他答案提供了通过 Runtime.getRuntime().totalMemory() 和 maxMemory() 执行的机制,但要非常小心 - 给出的答案仅在给定时刻为真。 After a GC, the totalMemory() will change (downwards!). GC 之后,totalMemory() 将改变(向下!)。 You can't have a perfectly accurate view of "how many live objects there are in the system" at all times - as that's exactly what GC calculates, and is expensive.您无法始终完全准确地了解“系统中有多少活动对象”——因为这正是 GC 计算的内容,而且成本很高。

Using JMX (See the GC bean, etc..) will help with that polling, but again, it's a sample over time.使用 JMX(参见 GC bean 等)将有助于轮询,但同样,它是一个随着时间推移的示例。

So I'm not sure what you're actually trying to solve here...所以我不确定你真正想在这里解决什么问题......

See runtime info:查看运行时信息:

Runtime.getRuntime().maxMemory();
Runtime.getRuntime().totalMemory();
Runtime.getRuntime().freeMemory();

Combination of jps and jstat command is really powerful enough to track your java memory details. jpsjstat命令的组合真的足够强大,可以跟踪你的 java 内存细节。

Use the jps command to get the pid of your java process and use jstat $pid to get the details of your java program.使用jps命令获取您的 java 进程的 pid 并使用jstat $pid获取您的 java 程序的详细信息。 Now you can run them in a loop and closely moniter the memory detatils of your java program.现在您可以循环运行它们并密切监视 Java 程序的内存详细信息。

You can find a bash implementation of this idea on github .你可以在github上找到这个想法的 bash 实现。 One big advantage of this script is that you can start this script, and then run your java program.这个脚本的一大优点是你可以启动这个脚本,然后运行你的java程序。 You will be able to see the memory details of your program from the very begning.您将能够从一开始就看到程序的内存详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM