java JVM 的内存占用是多少,如何将其最小化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38552250/
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
What is the memory footprint of the JVM and how can I minimize it?
提问by Martin Kersten
I just wanted to know what the actual footprint of the JavaVM (Sun, Linux) is when one starts to start to spawn multiple processes of JVMs. When I remember well those should share the rt.jar (and maybe beyond?). Does those JVM share the JIT cache (all JVM feature the same Classpath)?
我只是想知道 JavaVM(Sun、Linux)在开始生成多个 JVM 进程时的实际占用空间是多少。当我记得很清楚时,那些应该共享 rt.jar (也许更多?)。这些 JVM 是否共享 JIT 缓存(所有 JVM 都具有相同的类路径)?
Is there anything I can do to reduce the overhead of multi-instance JVM? (beside of setting smaller limits for the heap)?
我可以做些什么来减少多实例 JVM 的开销?(除了为堆设置较小的限制)?
Anything I can do while programming the application?
在编写应用程序时我可以做什么?
Can I share memory areas? Maybe share mapped memory blocks?
我可以共享内存区域吗?也许共享映射的内存块?
回答by apangin
This postdescribes what makes up a Java application footprint. That is, if you want to reduce footprint, you need to reduce those parts: Java Heap, Metaspace, Code Cache, direct buffers, number of threads etc.
这篇文章描述了 Java 应用程序足迹的组成部分。也就是说,如果您想减少占用空间,您需要减少这些部分:Java 堆、元空间、代码缓存、直接缓冲区、线程数等。
Instances of HotSpot JVM do not communicate to each other for sharing data. Basically they share nothing except for the things shared by the OS, i.e. the dynamic libraries (.so) and read-only memory-mapped files (.jars).
HotSpot JVM 的实例不会相互通信以共享数据。基本上,除了操作系统共享的内容,即动态库 (.so) 和只读内存映射文件 (.jars) 之外,它们不共享任何内容。
It's up to the application to provide further sharing via IPC mechanisms, e.g. memory-mapped files.
由应用程序通过 IPC 机制提供进一步的共享,例如内存映射文件。
回答by Average Joe
Probably it will be only a partial answer.
可能这只是部分答案。
What is the memory footprint of the JVM
JVM 的内存占用是多少
The full footprint of a Java app consists of Heap space and non-heap space (let me call it this way). Just a couple of examples of what resides in non-heap space: PermGen or Metaspace, derect allocation from code (malloc), NIO also uses native memeory.
Java 应用程序的完整占用空间由堆空间和非堆空间(让我这样称呼它)组成。举几个非堆空间的例子:PermGen 或 Metaspace,直接从代码分配(malloc),NIO 也使用本地内存。
how can I minimize it?
我怎样才能最小化它?
The heap usually occupies the biggest part of you footprint. So, I would start with it.
堆通常占据您足迹的最大部分。所以,我会从它开始。
As for the non-heap space, you can minimize: PermGen (If you max size is redundant), Thread stacks (They are quite large, especially in 64-bit JMMs) and Code cache (insted of performance, of course).
至于非堆空间,您可以最小化:永久代(如果最大大小是多余的)、线程堆栈(它们非常大,尤其是在 64 位 JMM 中)和代码缓存(当然是性能)。
Does those JVM share the JIT cache
那些 JVM 是否共享 JIT 缓存
In normal conditions (and I'm not aware of others) every process has its own footprint. That's actually what differs a process from a thread. And back to multiple JVMs, each JVM is a separate process.
在正常情况下(我不知道其他情况)每个过程都有自己的足迹。这实际上是进程与线程的不同之处。回到多个 JVM,每个 JVM 都是一个单独的进程。
should share the rt.jar
应该共享 rt.jar
If you start Java from the same directory (the same installation), of course, they share the same rt.jar, but only as a source of classes. For example, the String class will be loaded as many times as a number of running JVMs.
如果从同一个目录(同一个安装)启动 Java,当然它们共享同一个 rt.jar,但只是作为类的源。例如,String 类的加载次数与正在运行的 JVM 的加载次数一样多。
回答by Vadzim
To complement other answers, here is a couple of insightful articles listing typical JVM memory footprint values and ways to measure them:
为了补充其他答案,这里有几篇很有见地的文章,列出了典型的 JVM 内存占用值和衡量它们的方法:
https://spring.io/blog/2015/12/10/spring-boot-memory-performance
https://spring.io/blog/2015/12/10/spring-boot-memory-performance
http://trustmeiamadeveloper.com/2016/03/18/where-is-my-memory-java/
http://trustmeiamadeveloper.com/2016/03/18/where-is-my-memory-java/