Java 非堆内存和堆栈内存有什么区别?如果不是,它们是否相同,它们之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25867989/
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 difference between Java Non Heap Memory and Stack Memory? Are they Same if not what is the difference between them?
提问by user2885295
I am using Jconsole for monitoring a Java Application. The memory tab shows different Heap and Non Heap memories like
我正在使用 Jconsole 来监视 Java 应用程序。内存选项卡显示不同的堆和非堆内存,如
- Heap Memory Usage
- Non Heap Memory Usage
- Memory Pool "CMS Old Gen"
- Memory Pool "Par Eden Space"
- Memory Pool "Par Survivor Space"
- Memory Pool "Code Cache"
- Memory Pool "CMS Perm Gen"
- 堆内存使用
- 非堆内存使用
- 内存池“CMS Old Gen”
- 记忆池“Par Eden Space”
- 记忆池“Par Survivor Space”
- 内存池“代码缓存”
- 内存池“CMS Perm Gen”
What is the difference between these terms. Also please provide some information regarding - how to find anomalies in the application behavior by monitoring these parameters.
这些术语有什么区别。还请提供一些关于 - 如何通过监控这些参数来发现应用程序行为异常的信息。
采纳答案by Hot Licks
There are essentially three categories of storage in all C-based languages (and most other languages):
在所有基于 C 的语言(和大多数其他语言)中,基本上有三类存储:
- Heap
- Stack
- Static (with several variations)
- 堆
- 堆
- 静态(有多种变化)
Heap you're familiar with.
你熟悉的堆。
Stack you're also familiar with, but you just don't know it. When you have a method with "local" variables, those variables are allocated in a "invocation frame". The "invocation frame" is allocated when you call the method and deleted when you return from the method, and hence it's most efficiently implemented using a "stack" that grows with call and shrinks with return.
Stack 你也很熟悉,只是你不知道而已。当你有一个带有“局部”变量的方法时,这些变量被分配在一个“调用框架”中。“调用帧”在调用方法时分配,从方法返回时删除,因此使用“堆栈”最有效地实现,该“堆栈”随调用而增长并随返回而缩小。
Static is stuff that you don't explicitly allocate and essentially exists from the time program execution begins.
静态是你没有明确分配的东西,从程序执行开始就存在。
The space required for stack is generally fairly small and is lumped in with "Non Heap Memory" in the categories above.
堆栈所需的空间通常相当小,并与上述类别中的“非堆内存”混为一谈。
回答by Ahmet Karakaya
Please follow the links http://www.yourkit.com/docs/kb/sizes.jspand http://publib.boulder.ibm.com/infocenter/javasdk/v5r0/index.jsp?topic=%2Fcom.ibm.java.doc.diagnostics.50%2Fdiag%2Fproblem_determination%2Faix_mem_heaps.html
请按照链接http://www.yourkit.com/docs/kb/sizes.jsp和http://publib.boulder.ibm.com/infocenter/javasdk/v5r0/index.jsp?topic=%2Fcom.ibm .java.doc.diagnostics.50%2Fdiag%2Fproblem_determination%2Faix_mem_heaps.html
Non-Heap Also, the JVM has memory other than the heap, referred to as non-heap memory. It is created at the JVM startup and stores per-class structures such as runtime constant pool, field and method data, and the code for methods and constructors, as well as interned Strings.
Non-Heap 另外,JVM 拥有堆以外的内存,称为非堆内存。它在 JVM 启动时创建并存储每个类的结构,例如运行时常量池、字段和方法数据、方法和构造函数的代码以及内部字符串。
Unfortunately, the only information JVM provides on non-heap memory is its overall size. No detailed information on non-heap memory content is available.
不幸的是,JVM 提供的关于非堆内存的唯一信息是它的整体大小。没有关于非堆内存内容的详细信息。
The abnormal growth of non-heap memory size may indicate a potential problem, in this case you may check up the following:
非堆内存大小的异常增长可能表明存在潜在问题,在这种情况下,您可以检查以下内容:
If there are class loading issues such as leaked loaders. In this case, the problem may be solved with the help of Class loaders view. If there are strings being massively interned. For detection of such problem, Object allocation recording may be used.
如果存在类加载问题,例如加载器泄漏。在这种情况下,可以借助类加载器视图来解决问题。如果有字符串被大量拘留。为了检测这样的问题,可以使用对象分配记录。
回答by Marko Topolnik
Non-heap memory is all the memory the JVM allocated for purposes other than the heap. This includes:
非堆内存是 JVM 为堆以外的用途分配的所有内存。这包括:
- the call stacks (as you noted);
- memory allocated by native code (e.g. for off-heap caching);
- in HotSpot 8, the Metaspace (replacement for the Permanent Generation);
- memory used by the JIT compiler (compiled native code).
- 调用堆栈(如您所见);
- 本机代码分配的内存(例如用于堆外缓存);
- 在 HotSpot 8 中,Metaspace(永久代的替代品);
- JIT 编译器(编译的本机代码)使用的内存。
In your list, "CMS Old Gen", "Par Eden Space", "Par Survivor Space", and "CMS Perm Gen", all refer to various sections of the heap.
在您的列表中,“CMS Old Gen”、“Par Eden Space”、“Par Survivor Space”和“CMS Perm Gen”都指的是堆的各个部分。