Create object memory analysis

1. Stack

The area of ​​the Java stack is very small, about 2m, which is characterized by extremely fast access speed

​ Stack storage is characterized by first in and last out

​ Reasons for fast storage speed:

​ Stack memory, create and release space through the'stack pointer'!

​ Moving the pointer down will create new memory, and moving up will release these memory!

​ This method is extremely fast, second only to the PC register!

​ But in this way of movement, the size and scope of the movement must be clarified. The clarification of the size and scope is to facilitate the movement of the pointer. This is a limitation on data storage. The size of the stored data is fixed, which affects the flexibility of the program. Sex ~

​ So we store more of the data in the heap memory

What is stored is:

​ Data of basic data types and references to reference data types!

​ For example: int a =10;

​ Person p = new Person();

​ 10 is stored in the stack memory, and the reference to the object created by the second sentence of code § stored in the stack memory

2. Heap

It stores objects of the class.

Java is a pure object-oriented language, which limits the way objects are created:

所有类的对象都是通过new关键字创建

The new keyword means to tell the JVM that a new object needs to be created explicitly to open up a new heap memory space:

Heap memory is different from stack memory. The advantage is that when we create objects, we don't need to pay attention to how much storage space needs to be opened up in heap memory, and we don't need to pay attention to the length of memory usage!

The release of memory in the heap memory is done by the GC (garbage collector)

Rules for garbage collector to reclaim heap memory:

When there is no reference to this object in the stack memory, it will be regarded as garbage and wait for the garbage collector to collect it!

例如:
Person p0 = new Person();
Person p1 = p0;
Person p2 = new Person();

3. Method area

Stored is
-class information
-static variables
-constants
-member methods

The method area contains a special area (constant pool) (stored members are decorated with static)

4. PC register

The PC register holds the address of the JVM instruction currently being executed!

In a Java program, when each thread starts, a PC register is created!

5. Local method stack

Save the address of the native method!

Guess you like

Origin blog.csdn.net/weixin_43515837/article/details/110009793