Hello, tell me please. There are several Heap, Stack & Permanent Generation memory areas in Java. Here we start with the main method. We call him some methods. They have local variables and references that are stored on the stack. All is well. But let's say we create a new instance of the class inside the method. MyClass obj = new MyClass(); But this class has its own methods, variables (class members) of a primitive type. Let's say

 class MyClass { int x ; float y; MyClass inObj; } 

And there are also references to other objects (in this case, an object of the same class). Actually a question. Where local variables and references of the class selected from main will be stored. In Heap or Stack? Thank .

  • one
    @Alexandr Crospov um ... and why such subtleties to know a simple developer? I would understand if you wrote a compiler or there is something auxiliary for the axis - Barmaley
  • 3
    @Barmaley, you see, it is useful for a simple developer to know where OutOfMemoryError's legs grow from, and why some of them ask for the Java heap, and the other for PermGen. - Nofate

3 answers 3

As far as I understand (Java experts will correct me), everything that is allocated with the help of new is in the heap-memory. In particular, instances of classes also lie in heap, along with all its fields.

The class itself does not have local variables. Local variables are in his methods, these variables are placed on the stack when the method is called.

Possible deviations from this scheme (they are implemented in C #, I don’t know if it is in Java):

  • If the optimizer can prove that an instance of the class does not leave the limits of the method, it can move it onto the stack in a way that is invisible to the programmer for efficiency.
  • I don’t know if the compiler has the right to implicitly move local variables captured by the lambda function into heap.
  • one
    On the second point: creates a copy of the values ​​of captured local variables. - AlexeyM
  • @AlexeyM: Hmm, so you can only capture final ? - VladD
  • one
    In Java <= 7. Beginning with Java 8, the notion of effective final is introduced, i.e. which are not recorded. - AlexeyM
  • one
    @AlexeyM: Cool, did not know. Thank! - VladD

A reference to a class on the stack, an instance of the class itself in heap. In addition, with the -XX:+DoEscapeAnalysis flag -XX:+DoEscapeAnalysis and the -server key -server JVM can place instances of classes on the stack if these instances are not referenced outside the block (thus, the method optimized in this way must first be JIT compiled). See EscapeAnalysis .

  • @a_gura: formally, the link may fall into heap if it is a field of another class. I would do the separation according to the field / local variable criterion. - VladD
  • @VladD if the link is a field of another class, it is always in the heap (within the class itself), unless it is a local copy of the link or the escape analysis for the class containing it did not work. - a_gura

Stack / Stack - is in RAM (RAM) stores references to objects, as well as primitives.
Heap / Heap (also in RAM) - stores all objects (not links).
Permanent generation is used only by the JVM to store the necessary data, including metadata about the objects created.
Permanent Storage (ROM) - stores string literals and constants.
External storage - stores stream objects (stream in the form of bytes) and long-lived (persistent) objects (retain their state after the program is closed)