用于查找 Java 堆大小和使用的内存 (Linux) 的命令行工具?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12797560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:32:57  来源:igfitidea点击:

Command-line Tool to find Java Heap Size and Memory Used (Linux)?

javalinuxperformancememoryheap

提问by Jasper

Is there a Command-line Tool (Linux) to check Heap Size (and Used Memory) of a Java Application?

是否有命令行工具 (Linux) 来检查 Java 应用程序的堆大小(和已用内存)?

I have tried through jmap. But it gives info. about internal memory areas like Eden/ PermGen etc., which is not useful to me.

我已经通过 jmap 尝试过。但它提供了信息。关于 Eden/PermGen 等内部存储区域,这对我没有用。

I am looking for something like:

我正在寻找类似的东西:

  • Max Memory: 1GB
  • Min Memory: 256 MB
  • Heap Memory: 700 MB
  • Used Memory: 460 MB
  • 最大内存:1GB
  • 最小内存:256 MB
  • 堆内存:700 MB
  • 已用内存:460 MB

That's all. I know that I can see this in JConsole etc., but I need a command-line tool (can't enable JMX etc.)

就这样。我知道我可以在 JConsole 等中看到这一点,但我需要一个命令行工具(无法启用 JMX 等)

Do you know any such a tool/ command?

你知道任何这样的工具/命令吗?

回答by Peter Lawrey

Without using JMX, which is what most tools use, all you can do is use

不使用大多数工具使用的 JMX,您所能做的就是使用

jps -lvm

and infer that the settings will be from the command line options.

并推断设置将来自命令行选项。

You can't get dynamic information without JMX by default but you could write your own service to do this.

默认情况下,如果没有 JMX,您将无法获取动态信息,但您可以编写自己的服务来执行此操作。

BTW: I prefer to use VisualVM rather than JConsole.

顺便说一句:我更喜欢使用 VisualVM 而不是 JConsole。

回答by farmer1992

Each Java process has a pid, which you first need to find with the jpscommand.

每个 Java 进程都有一个pid,您首先需要使用jps命令找到它。

Once you have the pid, you can use jstat -gc [insert-pid-here]to find statistics of the behavior of the garbage collected heap.

获得 pid 后,您可以使用它jstat -gc [insert-pid-here]来查找垃圾收集堆行为的统计信息。

  • jstat -gccapacity [insert-pid-here]will present information about memory pool generation and space capabilities.

  • jstat -gcutil [insert-pid-here]will present the utilization of each generation as a percentage of its capacity. Useful to get an at a glance view of usage.

  • jstat -gccapacity [insert-pid-here]将提供有关内存池生成和空间功能的信息。

  • jstat -gcutil [insert-pid-here]将显示每一代的利用率为其容量的百分比。有助于一目了然地了解使用情况。

See jstat docson Oracle's site.

请参阅Oracle 站点上的jstat 文档

回答by MRalwasser

jvmtopis a command-line tool which provides a live-view at several metrics, including heap.

jvmtop是一个命令行工具,它提供多个指标的实时视图,包括堆。

Example output of the VM overview mode:

VM 概览模式的示例输出:

 JvmTop 0.3 alpha (expect bugs)  amd64  8 cpus, Linux 2.6.32-27, load avg 0.12
 http://code.google.com/p/jvmtop

  PID MAIN-CLASS      HPCUR HPMAX NHCUR NHMAX    CPU     GC    VM USERNAME   #T DL
 3370 rapperSimpleApp  165m  455m  109m  176m  0.12%  0.00% S6U37 web        21
11272 ver.resin.Resin [ERROR: Could not attach to VM]
27338 WatchdogManager   11m   28m   23m  130m  0.00%  0.00% S6U37 web        31
19187 m.jvmtop.JvmTop   20m 3544m   13m  130m  0.93%  0.47% S6U37 web        20
16733 artup.Bootstrap  159m  455m  166m  304m  0.12%  0.00% S6U37 web        46

回答by guest

If using jrockit try the jrcmd command line tool. For example:

如果使用 jrockit,请尝试 jrcmd 命令行工具。例如:

$ jrcmd 5127 print_memusage
5127:
Total mapped                  1074596KB           (reserved=3728KB)
-              Java heap       786432KB           (reserved=0KB)
-              GC tables        26316KB          
-          Thread stacks        13452KB           (#threads=34)
-          Compiled code         9856KB           (used=9761KB)
-               Internal          840KB          
-                     OS        15036KB          
-                  Other       146632KB          
-        Java class data        75008KB           (malloced=74861KB #103221 in 18709 classes)
- Native memory tracking         1024KB           (malloced=102KB #8)

For more commands, like heap_diagnostics, use "jrcmd help" to list them.

对于更多命令,如 heap_diagnostics,请使用“jrcmd help”列出它们。

https://blogs.oracle.com/jrockit/entry/why_is_my_jvm_process_larger_t

https://blogs.oracle.com/jrockit/entry/why_is_my_jvm_process_larger_t

回答by Atma

This command shows the configured heap sizes in bytes.

此命令以字节为单位显示配置的堆大小。

java -XX:+PrintFlagsFinal -version | grep HeapSize

It works on Amazon AMI on EC2 as well.

它也适用于 EC2 上的 Amazon AMI。

回答by M. Mashaye

Using topcommand is the simplest way to check memory usage of the program. REScolumn shows the real physical memory that is occupied by a process.

使用top命令是检查程序内存使用情况的最简单方法。RES列显示进程占用的实际物理内存。

For my case, I had a 10g file read in java and each time I got outOfMemory exception. This happened when the value in the REScolumn reached to the value set in -Xmxoption. Then by increasing the memory using -Xmxoption everything went fine.

就我而言,我在 java 中读取了一个 10g 文件,并且每次出现 outOfMemory 异常。当RES列中的值达到-Xmx选项中设置的值时会发生这种情况。然后通过使用-Xmx选项增加内存,一切顺利。

回答by padippist

Try this it worked in Ubuntu and RedHat:

试试这个它在 Ubuntu 和 RedHat 中工作:

java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'

For Windows:

对于 Windows:

java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"

For Mac

对于 Mac

java -XX:+PrintFlagsFinal -version | grep -iE 'heapsize|permsize|threadstacksize'

The output of all this commands resembles the output below:

所有这些命令的输出类似于以下输出:

uintx InitialHeapSize                          := 20655360        {product}
uintx MaxHeapSize                              := 331350016       {product}
uintx PermSize                                  = 21757952        {pd product}
uintx MaxPermSize                               = 85983232        {pd product}
intx ThreadStackSize                           = 1024            {pd product}
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

To find the size in MB, divide the value with (1024*1024).

要找到以 MB 为单位的大小,请将值除以 (1024*1024)。

回答by Pravin

jstat -gccapacity javapid  (ex. stat -gccapacity 28745)
jstat -gccapacity javapid gaps frames (ex.  stat -gccapacity 28745 550 10 )

Sample O/P of above command

上述命令的示例 O/P

NGCMN    NGCMX     NGC     S0C  
87040.0 1397760.0 1327616.0 107520.0 

NGCMN   Minimum new generation capacity (KB).
NGCMX   Maximum new generation capacity (KB).
NGC Current new generation capacity (KB).

Get more details about this at http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jstat.html

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jstat.html获取更多详细信息

回答by Yuci

In terms of Java heap size, in Linux, you can use

在Java堆大小方面,在Linux中,可以使用

ps aux | grep java

or

或者

ps -ef | grep java

and look for -Xms, -Xmx to find out the initial and maximum heap size specified.

并查找 -Xms, -Xmx 以找出指定的初始和最大堆大小。

However, if -Xms or -Xmx is absent for the Java process you are interested in, it means your Java process is using the default heap sizes. You can use the following command to find out the default sizes.

但是,如果您感兴趣的 Java 进程不存在 -Xms 或 -Xmx,则意味着您的 Java 进程正在使用默认堆大小。您可以使用以下命令找出默认尺寸。

java -XX:+PrintFlagsFinal -version | grep HeapSize

or a particular jvm, for example,

或特定的 jvm,例如,

/path/to/jdk1.8.0_102/bin/java -XX:+PrintFlagsFinal -version | grep HeapSize

and look for InitialHeapSize and MaxHeapSize, which is in bytes.

并查找以字节为单位的 InitialHeapSize 和 MaxHeapSize。

回答by Andrejs

There is a command line tool with a visual aspect - jvm-mon. It is a JVM monitoring tool for the command line that disaplys:

有一个具有可视化方面的命令行工具 - jvm-mon。它是一个用于命令行的 JVM 监控工具,它显示:

  • heap usage, size and max
  • jvm processes
  • cpu and GC usage
  • top threads
  • 堆使用、大小和最大值
  • jvm进程
  • cpu 和 GC 使用率
  • 顶线

The metrics and charts update while the tool is open.

工具打开时,指标和图表会更新。

Sample: jvm-mon

样本: jvm-mon