Focus on “Java Backend Technology Full Stack”

Respond to “000” to get lots of e-books

The stack to heap

If there is a variable in the stack frame of type reference, such as:

package com.tian.my_code.test; public class JvmCodeDemo { public Object testGC(){ int op1 = 10; int op2 = 3; Object obj = new Object(); Object result=obj; return result; }}

If obj points to an Object in the stack, result points to the same Object as obj.

Using the command

javac -g:vars JvmCodeDemo.java

Compile it and use it again

javap -v JvmCodeDemo.class >log.txt

Then open the log.txt file

If you are still in doubt, you can combine the bytecode diagram and diagram above to make it easier to understand.

The method area points to the heap

Static variables, constants and other data are stored in the method area.

In a typical method area, an element points to an object in the heap.

The heap points to the method area

The method area contains information about the class, and the object is stored in the heap. To create an object, there is the corresponding class information, and this class information is in the method area.

Java object memory layout

A Java object has three parts in memory: the object header, the instance data, and the alignment population

Recommended reading:

The Art of Concurrent Programming in Java. PDF

Forty-five Habits of Highly Effective Programmers: The Instruction of Agile Development. PDF

The Java Web Enterprise Project in Action. PDF

Pay attention to the public number “Java back-end technology full stack”

Free access to 500GB of the latest learning materials