java memory and data area

The data area of ​​the Java runtime includes: (the first two are shared by threads)

  • 1. Method Area (Method Area) Stores data such as class information, constants, static variables, and code compiled by the real-time compiler that have been loaded by the virtual machine

  • 2. The heap (Heap) stores object instances, almost all object instances allocate memory here

  • 3. The virtual machine stack (VM Stack) describes the memory model of Java method execution: each method will create a Stack Frame (basic data structure when the method runs) when it is executed to store the local variable table and operand stack. , dynamic connection, method exit, etc.

  • 4. Native Method Stack is similar to the virtual machine stack, but serves the native methods used by the virtual machine. (Some virtual machines, such as the Sun HotSpot virtual machine, directly combine the local method stack and the virtual machine stack into one)

  • 5. Program Counter Register can be regarded as an identifier of the line number of the bytecode executed by the current thread


The overall JVM memory is divided into 4 parts:

1、stack segment
2、heap segment
3、segment code
4、data segment)
  • When we declare a local variable in the program, this variable is stored in the stack segment (stack);

  • When an object is new, the object is placed in the heap segment (heap);

  • Static variables or string constants exist in the data segment (data area);

  • The methods in the class exist in the code segment (code area).


The memory of the java virtual machine can be divided into three areas:

  • stack stack

    • Features:
      • (1) The stack describes the memory model of method execution, and each method is called to create a stack frame (storing local variables, operands, method entries, etc.);
      • (2) JVM creates a stack for each thread to store the information of the thread execution method (actual parameters, local variables, etc.);
      • (3) The stack is private to threads and cannot be shared between threads;
      • (4) The storage feature of the stack is FIFO, LIFO ;
      • (5) The stack is automatically allocated by the system, and the speed is fast. The stack is a continuous memory space.
  • heap heap

    • Features:
      • (1) The heap is used to store created objects and arrays (arrays are also objects);
      • (2) The JVM has only one heap, which is shared by all threads;
      • (3) The heap is a discontinuous memory space with flexible allocation and slow speed.
  • Method area method area (located in the heap)

    • Features:
      • (1) JVM has only one method area, which is shared by all threads;
      • (2) The method area is actually a heap, but it is only used to store information related to classes and constants;
      • (3), use the content that is always unchanged or unique in the storage program. (Class information [Class object], static variables, string constants, etc.)

java runtime data area

  • The runtime data area includes: virtual machine stack area, heap area, method area, local method stack, program counter

    • Virtual machine stack area: also known as the stack area, which is private to threads, stores basic types, object references and returnAddress, and completes the allocation during compilation.

    • The heap area, the JAVA heap, also known as the GC heap, is shared by all threads and stores instances and arrays of objects. The JAVA heap is the main area managed by the garbage collector.

    • Method area: shared by all threads, storing data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine. The memory recovery goals of this area are mainly for the recovery of objects in the constant pool and the unloading of types.

    • Program counter: Thread private, each thread has its own independent program counter, which is used to indicate the address of the next instruction.

java memory model

  • Communication between Java threads is governed by the Java Memory Model (JMM for short), which determines when a thread's writes to a shared variable are visible to another thread.

    • From an abstract point of view, JMM defines the abstract relationship between threads and main memory: shared variables between threads are stored in main memory (main memory), and each thread has a private local memory (local memory) , a copy of the thread to read/write shared variables is stored in local memory. Local memory is an abstraction of JMM and does not really exist. It covers caches, write buffers, registers, and other hardware and compiler optimizations
    • The write-read of volatile variables enables communication between threads.
    • From a memory semantics point of view, volatile has the same effect as monitor locks: volatile writes have the same memory semantics as monitor releases; volatile reads have the same memory semantics as monitor acquires.

accumulation of knowledge

  • 1. String str1 = "abc", "abc" is allocated in the memory character constant area;

  • 2. The program counter is a relatively small memory area, which is used to indicate the line of the bytecode executed by the current thread, which is thread isolated;

  • 3. The virtual machine stack describes the memory model of Java method execution, which is used to store local variables, operand stacks, dynamic links, method exits and other information, and is thread isolated;

  • 4. In principle, all objects are allocated memory on the heap area, which is shared between threads

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066606&siteId=291194637