简体   繁体   English

JVM 中的栈/堆

[英]Stack/Heap in JVM

I come from C/C++ background, where a process memory is divided into:我来自C/C++背景,其中一个进程内存分为:

  • Per thread stack每个线程堆栈
  • Heap
  • Instructions指示
  • Data数据

I am trying to understand how JVM works, I looked at different resources, I gathered that the JVM memory is divided into heap and stack as well plus few other things.我试图了解 JVM 是如何工作的,我查看了不同的资源,我发现 JVM 内存分为堆和堆栈以及其他一些东西。

I want to wrap my mind around this, when I read heap and stack in JVM are we talking about concepts of stack and heap?我想围绕这一点,当我在 JVM 中阅读堆和堆栈时,我们在谈论堆栈和堆的概念吗? and that the actual memory of the entire JVM resides on the heap (and here I mean the C++ concept of a Heap)?并且整个 JVM 的实际内存驻留在堆上(这里我指的是堆的 C++ 概念)?

I want to wrap my mind around this, when I read heap and stack in JVM are we talking about concepts of stack and heap?我想围绕这一点,当我在 JVM 中阅读堆和堆栈时,我们在谈论堆栈和堆的概念吗?

Yes, in general this is the case.是的,一般情况就是这样。 Each thread has its own per-thread stack, which is used to store local variables in stack frames (corresponding to method calls).每个线程都有自己的每线程堆栈,用于在堆栈帧(对应于方法调用)中存储局部变量。 The stack need not be located in a location related to the per-thread stack at the OS level.堆栈不需要位于与 OS 级别的每线程堆栈相关的位置。 If the stack attempts to grow past a size as specified by -Xss or a default set by the implementation, a StackOverflowError will be thrown.如果堆栈尝试增长超过-Xss指定的-Xss或实现设置的默认值,则会抛出StackOverflowError

The stack can exist in C/C++ heap memory, and need not be contiguous ( JVM spec v7 ):堆栈可以存在于 C/C++ 堆内存中,并且不需要是连续的( JVM 规范 v7 ):

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread.每个 Java 虚拟机线程都有一个私有的 Java 虚拟机堆栈,与线程同时创建。 A Java Virtual Machine stack stores frames (§2.6). Java 虚拟机堆栈存储帧(第 2.6 节)。 A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Java 虚拟机堆栈类似于 C 等传统语言的堆栈:它保存局部变量和部分结果,并在方法调用和返回中发挥作用。 Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated.因为除了推送和弹出帧之外,Java 虚拟机堆栈从不直接操作,所以帧可能被分配到堆上。 The memory for a Java Virtual Machine stack does not need to be contiguous. Java 虚拟机堆栈的内存不需要是连续的。

The Java heap is a means of storing objects, including automatic garbage collection when objects are no longer reachable via strong references. Java 堆是一种存储对象的方法,包括当对象不​​再可通过强引用访问时的自动垃圾收集。 It is shared between all threads running on a JVM.它在 JVM 上运行的所有线程之间共享。

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. Java 虚拟机有一个在所有 Java 虚拟机线程之间共享的堆。 The heap is the run-time data area from which memory for all class instances and arrays is allocated.堆是运行时数据区,从中分配所有类实例和数组的内存。

The heap is created on virtual machine start-up.堆是在虚拟机启动时创建的。 Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector);对象的堆存储由自动存储管理系统(称为垃圾收集器)回收; objects are never explicitly deallocated.对象永远不会被显式释放。 The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements. Java 虚拟机不假设任何特定类型的自动存储管理系统,存储管理技术可以根据实现者的系统需求来选择。 The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary.堆可以是固定大小的,也可以根据计算的需要进行扩展,如果不需要更大的堆,则可以收缩。 The memory for the heap does not need to be contiguous.堆的内存不需要是连续的。

By simply calling a constructor (eg HashMap foo = new HashMap() ) the JVM will allocate the requisite memory on the heap for this object (or throw an OutOfMemoryError if that is not possible).通过简单地调用构造函数(例如HashMap foo = new HashMap() ),JVM 将为该对象在堆上分配必要的内存(如果不可能,则抛出 OutOfMemoryError)。 It's also important to note that objects never live on the stack--only references to them do.同样重要的是要注意对象永远不会存在于堆栈中——只有对它们的引用才会存在。 Additionally, non-primitive fields also always contain references to objects.此外,非原始字段也始终包含对对象的引用。

It's also possible to allocate memory off-heap through sun.misc.Unsafe on some JVMs, some NIO classes that allocate direct buffers, and through the use of JNI.也可以通过某些 JVM 上的sun.misc.Unsafe 、某些分配直接缓冲区的 NIO 类以及通过使用 JNI 来在堆外分配内存。 This memory is not part of the JVM heap and does not undergo automatic garbage-collection (meaning that it would need to be released through means such as delete , but it may be a part of heap memory as C++ may refer to it.该内存不是 JVM 堆的一部分,不会进行自动垃圾回收(意味着它需要通过诸如delete类的方式释放,但它可能是堆内存的一部分,因为 C++ 可能会引用它。

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

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