java - 限制 ubuntu 上的 jvm 进程内存

标签 java linux memory jvm

我知道有同样的问题,例如 this one被问及回答。我对答案不满意,所以让我在这里输入更多详细信息。

我尝试使用 JVM OPT 启动我的应用程序:-Xmx128m -Xms32m -XX:MaxPermSize=64m。当应用程序启动时,我通过键入 cat/proc/10413/status 检查内存使用情况,我发现 vmsize 超过 600512 kB!这比我的设置大得多。我想知道如何限制进程的jvm内存使用。

Name:   java
State:  S (sleeping)
Tgid:   10413
Pid:    10413
PPid:   1
TracerPid:      0
Uid:    1001    1001    1001    1001
Gid:    1007    1007    1007    1007
FDSize: 128
Groups: 1001 1007
**VmPeak:   728472 kB**
**VmSize:   600512 kB**
VmLck:         0 kB
VmHWM:    298300 kB
VmRSS:    280912 kB
VmData:   647804 kB
VmStk:       140 kB
VmExe:        36 kB
VmLib:     13404 kB
VmPTE:       808 kB
VmSwap:        0 kB
Threads:        33
SigQ:   0/31522
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 2000000181005ccf
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: ffffffffffffffff
Cpus_allowed:   f
Cpus_allowed_list:      0-3
Mems_allowed:   00000000,00000001
Mems_allowed_list:      0
voluntary_ctxt_switches:        3
nonvoluntary_ctxt_switches:     2

最佳答案

你无法控制你想控制的东西-Xmx只控制Java Heap,它不控制 native 内存的消耗 由 JVM 执行,根据实现的不同,它们的使用方式完全不同。

来自以下文章Thanks for the Memory ( Understanding How the JVM uses Native Memory on Windows and Linux )

维护堆和垃圾收集器使用您无法控制的 native 内存。

More native memory is required to maintain the state of the memory-management system maintaining the Java heap. Data structures must be allocated to track free storage and record progress when collecting garbage. The exact size and nature of these data structures varies with implementation, but many are proportional to the size of the heap.

JIT 编译器使用 native 内存,就像 javac 一样

Bytecode compilation uses native memory (in the same way that a static compiler such as gcc requires memory to run), but both the input (the bytecode) and the output (the executable code) from the JIT must also be stored in native memory. Java applications that contain many JIT-compiled methods use more native memory than smaller applications.

然后你就有了使用 native 内存的类加载器

Java applications are composed of classes that define object structure and method logic. They also use classes from the Java runtime class libraries (such as java.lang.String) and may use third-party libraries. These classes need to be stored in memory for as long as they are being used. How classes are stored varies by implementation.

我什至不会开始引用线程部分,我想你明白了 -Xmx 不控制你认为它控制的东西,它控制 JVM 堆,而不是所有东西 进入 JVM 堆,堆占用的 native 内存比您指定的要多 管理和簿记。

关于java - 限制 ubuntu 上的 jvm 进程内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9145769/

相关文章:

linux - 在 :0 instead of :1 上启动 vncserver

maven - 配置 GWT 编译器以使用更少的内存?

c++ - static cast 和 new 的区别

java - 为什么不能在 java.lang 中创建类?

java - 这是一种策略模式还是回调?

linux - 将控制台输出重定向到 unix 中的文件

python - 如何使 pexpect 命令在我的程序中没有 interact() 的情况下执行

c# - 加载 wav 文件或使用嵌入式资源的内存效率更高?

c# - 从外部应用程序读取屏幕上的文本。 API Hook ?

java - 在接口(interface)上使用 .getclass()