• Runtime data area
    • Program counter
    • Java virtual machine stack
      • The stack frame
    • Local method stack
    • The heap
    • Methods area
      • Run-time constant pool
  • expand

Runtime data area

During the execution of Java programs, the Java VIRTUAL machine divides the memory it manages into different data areas.



Program counter

  • 1. The Program Counter Register is a small memory space that can be regarded as a line number indicator of the bytecode executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to execute.

  • 2. Bytecode instructions, branches, loops, jumps, exception handling, thread recovery and other basic functions rely on this counter.

  • 3. Each thread has an independent program counter, counters between each thread do not affect each other, independent storage. As shown in the figure above, we call this type of memory area: thread private memory.

  • 4. If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed. If the Native method is being executed, this counter value is null (Undefined).

  • 5. This memory region is the only one in the Java Virtual machine that does not regulate any OutOfMemoryError cases.

Java virtual machine stack

  • 1. The Java virtual machine stack is also thread-private and has the same life cycle as a thread (live with the thread, die with the thread)

  • 2. StackOverflowError is raised if the thread request stack depth is greater than the virtual machine allows. If the virtual stack can be extended dynamically, OutOfMemoryError will be raised if sufficient memory cannot be allocated. (Most current JVMS can scale dynamically, although the JVM specification also allows a fixed-length virtual stack.)

  • 3. The Java virtual machine stack describes the memory model of Java method execution: a stack frame is created for each method execution. For us, the main concern of the stack memory is the local variable table part of the virtual machine stack.

The stack frame

A stack frame is a data structure used to support virtual machine method invocation and method execution. It is the stack element of the virtual machine stack in the data area when the virtual machine is running. Stack frames are used to store information about local variables, operand stacks, dynamic links, method returns, etc. The process of each method from invocation to completion corresponds to the process of a stack frame in and out of the virtual machine stack. In an active thread, only the stack frame at the top of the stack is valid, called the current stack frame, and the method associated with this stack frame is called the current method. All bytecode instructions run by the execution engine operate only on the current stack frame.



Let’s look at the local variable table a little bit, and we’ll focus on the stack structure later. The local variable table stores the basic data types known at compile time (Boolean, byte, char, short, int, float, long, double). The object reference type and the returnAddress type (which points to the address of a bytecode instruction) are stored in stacks, as many sources on the Trivia web refer to basic data and object references. Of course, this statement is correct, but it is not precise, it can only be said that this statement is aimed at local variables. Local variables are stored in the local variable table and are born and die with the thread. And data is not shared between threads. However, if they are member variables, or references to objects defined outside the method, they are stored in the heap. Because in the heap, threads share data, and the name in the stack frame clearly demarcates the boundary: local variable table!

Local method stack

  • Native Method Stack and virtual machine Stack play very similar roles. The difference between them is that the virtual machine Stack performs Java Method services (that is, bytecode) services for virtual machines, while the Native Method Stack serves the Native methods used by virtual machines.

  • The Java Virtual Machine specification does not mandate the language, usage, or data structure of the native method stack, so it can be implemented freely by the virtual machine. For example, the HotSpot VIRTUAL machine directly combines the local method stack with the virtual machine stack.

  • As with the virtual machine stack, the Java Virtual Machine specification specifies two exceptions for this area: StackOverflowError and OutOfMemoryError.

The heap

  • 1. The Java heap is an area of memory shared by all threads. It is created when the VIRTUAL machine is started and is the largest chunk of memory managed by the virtual machine. The only purpose of this memory area is to hold object instances and arrays. Almost all object instances and arrays are allocated in this area.

  • 2. The Java heap is the primary area managed by the garbage collector, also known as the GC garbage heap. The GC algorithm will be analyzed exclusively later. From the point of view of memory collection, the Java heap can be subdivided into: new generation, old generation; From a memory allocation perspective, a Java heap shared by threads can be divided into multiple thread private allocation buffers (TLabs); No matter how you divide it up, it doesn’t matter what you store. In any region, you still store object instances and arrays.

  • 3. OutOfMemoryError is raised if there is no memory in the heap to complete the instance allocation and the heap cannot be extended.

  • 4. Memory leak and overflow Memory leak: Memory is allocated dynamically to temporary objects in a program, but the objects are not collected by the GC. That is, the allocated objects are reachable but no longer useful, and there is less and less memory available. Memory overflow: An error that occurs when a program fails to allocate enough memory. A memory overflow usually occurs when there is no memory space for new Java objects after the old generation or permanent generation garbage collection. Memory leaks are a cause of memory overflow, not the only one.

Methods area

  • 1. The method area, also known as the static area, is always the only element storage area in a program. Like the heap, is an area of memory shared by individual threads. It is used to store information about classes that have been loaded by the virtual machine, constants, static variables, code compiled by the just-in-time compiler, and so on.

  • 2. The Java VIRTUAL Machine specification is very relaxed about method areas, in addition to the fact that, like the Java heap, it does not require continuous memory and can choose to be fixed size or extensible, it can also choose not to implement garbage collection. The target of memory reclamation in this area is mainly for constant pool reclamation and type unloading. Generally speaking, the memory reclamation in this area is not satisfactory, especially for type reclamation, the conditions are quite harsh, but the memory reclamation in this area is indeed necessary.

  • 3. Many developers prefer to refer to method areas as “Perm Gen” (Permanent Generation) “always store things that don’t change very easily”. In HotSpot, which has been released in JDK 1.7 so far, the pool of string constants that used to be in the persistent generation has been moved to the heap.

  • 4. The Runtime Constant Pool is part of the method area.

Run-time constant pool

1. The Constant Pool Table is used to store literal and symbolic references generated at compile time. This part of the Constant Table is stored in the runtime Constant Pool after the Class is loaded into the method area.

  • Literals: Close to the Java language level of constant concepts, such as text strings, constant values declared final, etc. Final modifies member variables and class variables! “Class variables are static (member) variables,” which are local variables except for final modifications.

  • Symbolic references: Concepts that are aspects of compilation principles, including 1. Fully qualified names of classes and interfaces (i.e., paths, package names + class names). 2. Field name and descriptor. 3. Method name and descriptor. When the virtual machine is running, the corresponding symbolic quotes need to be retrieved from the constant pool and parsed and translated into a specific memory address during class creation or runtime (direct reference).

2. In addition to saving symbolic references described in the Class file, compiled direct references are also stored in the runtime constant pool.

3. The Java language does not require that constants be generated only at compile time. That is, a constant that is not in the constant pool of a Class file can be added to the method area’s runtime constant pool. (We’ll examine the String class later.)

4. OutofMemoryError is thrown when the constant pool cannot allocate memory.







expand

Member variables and local variables

  • Member variables: variables defined outside a method but inside a class;

  • Local variable: a variable defined inside a method or statement block that must be initialized.

Parameters are local variables, and arguments can be local or global variables in a method. Local variables in stack memory are born and die with the method. Member variables are stored in objects in the heap and collected by the garbage collector.

define Member variables A local variable A static variable
Define the location In a class, outside of a method Method, or parameter Class, outside of methods (Java only has static member variables)
The initialization value There are default initialization values None. The value is defined before it is assigned Default initialization value
Storage location The heap The stack Methods area
The statement period Live or die with the object Live or die with methods (threads) Live or die with the class
The alias The instance variables Class variables