[JVM] 01 Virtual Machine Memory Model

Learning link: https://blog.csdn.net/u010425776/article/details/51170118

The blogger's arrangement is clear, thank the blogger for sharing here


Last year, I watched a video to learn and wrote a blog about JVM. This year, I have forgotten all about it. In the final analysis, I did not understand it in depth with practice.

JVM memory model

The memory space of the Java Virtual Machine (Java Virtual Machine=JVM) is divided into five parts, namely:

1. Program Counter
2. Java Virtual Machine Stack
3. Native Method Stack
4. Heap

5. Method area .

1. Program counter:

1.1 What is a program counter:

    The program counter is a small memory space that records the line number of the bytecode file executed by the current thread

1.2, the role of the program counter:

    1. The bytecode interpreter reads instructions in sequence by changing the program counter to realize code flow control, such as sequential execution, looping, etc.

    2. In the case of multi-threading, record the position where the current thread executes, so that it can continue to execute when the thread is activated.

1.3, the characteristics of the program counter:

    1. It is a small storage space

    2. Thread private, each thread has its own program counter

    3. It is the only memory area that does not appear OutOfMemoryError

     4. The life cycle is created with the creation of the thread, and destroyed with the ready-made destruction


2. Java Virtual Machine Stack (JVM Stack)

2.1 What is the java virtual machine stack:

The Java virtual machine stack is a memory model that describes the running process of a method

2.2 The role of the java virtual machine stack:

The Java virtual machine stack allocates memory for each method that is about to run, called a stack frame, to store some information needed during the running process.

include:

1. Local variable table (basic data type, reference data type variable (memory address?), returnAddress (never heard of it))

2. Operand stack

3. Dynamic link

4. Method export information

5. Wait

2.3 Process and Features:

When a method is about to be executed, the Java virtual machine stack will first create a "stack frame" for the method in the Java virtual machine stack. The stack frame contains the local variable table, operand stack, dynamic link, method exit information, etc. When the method needs to create a local variable during the running process, the value of the local variable is stored in the local variable table of the stack frame (incomprehensible).
When this method is executed, the stack frame corresponding to this method will be popped off the stack and the memory space will be released.

Note: People often say that Java's memory space is divided into "stack" and "heap". The stack stores local variables and the heap stores objects.

This sentence is not entirely correct! The "heap" here can be understood in this way, but the "stack" here only represents the local variable table part of the Java virtual machine stack. The real Java virtual machine stack is composed of stack frames, and each stack frame has: local variable table, operand stack, dynamic link, method exit information.


3. Native method stack


Similar to the virtual machine stack, it seems to be a local method balabala for native decoration~


4. Heap

4.1 What is the role of heap and heap:

The heap is the memory space used to store objects, and almost all objects are stored in the heap.

4.2 Features of the heap:

1. Thread sharing:

There is only one heap for the entire Java virtual machine, and all threads access the same heap. The program counter, the Java virtual machine stack, and the native method stack are all corresponding to one thread.

2. Created when the virtual machine starts

3. The main place for garbage collection

4. It can be further subdivided into the new generation and the old generation (this remembers that there have been changes after JDK7 and before). The new generation can be divided into Eden (Eden, the meaning of the starting point?), from servior, to servior. Different areas store objects of different periods. The virtual machine adopts different garbage collection algorithms according to different areas, which is more efficient.

5. The mainstream virtual machine heaps are scalable. When a thread requests the virtual machine to allocate memory, but the heap is full, an outofmemoryError will be thrown



5. Method area

5.1. What is the method area:

The Java Virtual Machine Specification defines the method area as a logical part of the heap.

The method area stores class information loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler.

5.2. Features of method area:

1. Thread sharing: Since it is part of the heap, it is shared by threads like the heap, and the entire virtual machine has only one method area.

2. Permanent generation: The data in the method area generally exists for a long time, and it is a logical partition of the heap, so according to the method of dividing the heap, we call the method area the old generation .

3. The data in the method generally exists for a long time, and the garbage collector can only recycle a small amount of data at a time, so the garbage collection strategy is mainly to recycle the constant pool and unload the type.

4. The Java virtual machine specification has relatively loose requirements on the method area, allowing fixed or extended size, and also allowing no garbage collection.

5.3. Runtime constant pool

Three kinds of data are stored in the method area: class information, constants, static variables, and code compiled by the real-time compiler (what is this). where constants are stored in the runtime constant pool.

We generally declare a constant in a class through public static final. After this class is compiled, a Class file is generated, and all the information of this class is stored in this class file.

When the class is loaded by the Java virtual machine, the constants in the class file are stored in the runtime constant pool of the method area. And during runtime, new constants can be added to the constant pool. For example, the intern() method of the String class can add string constants to the constant pool during runtime.

When some constants in the runtime constant pool are not referenced by objects and are not referenced by variables, they need to be collected by the garbage collector.

6. Direct memory

Direct memory is memory other than the Java virtual machine, but may also be used by Java.

A channel and buffer-based IO method (can't understand) is introduced in NIO. It can directly allocate memory outside the Java virtual machine by calling a native method, and then directly operate the memory through a DirectByteBuffer object stored in the Java heap, without first copying the data in the external memory to the heap and then operating, thereby improving efficiency of data manipulation.

The size of direct memory is not controlled by the Java virtual machine, but since it is memory, an OOM exception will be thrown when the memory is insufficient.


The process of creating an object and the process of accessing an object


The process of creating objects: https://blog.csdn.net/u010425776/article/details/51190801

Excerpt from the object memory model:

object creation process

When the virtual machine encounters an instruction containing new, it will perform a series of object creation operations:

  1. Check whether there is a symbolic reference to the class to which the object to be created belongs in the constant pool;

    • If there is no symbolic reference to this class in the constant pool, it means that this class has not been defined! throw ClassNotFoundException;
    • If there is a symbolic reference to this class in the constant pool, proceed to the next step;
  2. Then check whether the class represented by this symbolic reference has been loaded by the JVM;

    • If the class has not been loaded, find the class file of the class and load it into the method area;
    • If the class has been loaded by the JVM, prepare to allocate memory for the object;
  3. Determine the memory size required by the class according to the information of the class in the method area; the memory size
    required by an object can be determined after the class to which the object belongs is defined! And the memory size of all objects produced by a class is the same! When a class is loaded into the method area, the JVM knows the memory size required for each object produced by the class.

  4. Divide a piece of memory space of the corresponding size from the heap to the new object;
    there are two ways to allocate memory in the heap:

    • Pointer Collision
      If the JVM's garbage collector uses a copy algorithm or a mark-to-groom algorithm, then the free memory in the heap is a complete area, and the free memory and the used memory are marked by a pointer. Then when allocating memory for an object, just move the pointer. Therefore, this way of allocating memory by moving a pointer over a completely free area is called "pointer collision".
    • Free list
      If the JVM's garbage collector adopts the mark-sweep algorithm, the free area and the used area in the heap are interleaved, so a "free list" is needed to record which areas in the heap are free areas, so that when objects are created Find free areas according to this "free list" and allocate memory.
      To sum up: which memory allocation method the JVM uses depends on which garbage collector it uses.
  5. Assign initial values ​​to member variables in the object (default initialization);

  6. Set the information in the object header;

  7. Call the object's constructor for initialization
    At this point , the entire object creation process is complete.


process of accessing objects

We know that the variable of reference type stores an address, so according to the type of address, the object has different access methods:

  1. Handle access method There needs to be a memory space called "handle pool" in the
    heap , which is used to store the addresses of all objects and the class information of the classes to which all objects belong.
    The variable of reference type stores the address of the object in the handle pool. When accessing an object, you first need to find the handle of the object through a variable of reference type, and then access the object according to the address of the object in the handle.

  2. Direct pointer access mode The variable of
    reference type directly stores the address of the object, so that the handle pool is not needed, and the object can be directly accessed by reference.
    However, an additional strategy is required in the memory space where the object is located to store the address of the class information to which the object belongs.



Guess you like

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