Home  >  Article  >  Java  >  Analysis of the relationship between stack memory, heap memory and method area

Analysis of the relationship between stack memory, heap memory and method area

王林
王林forward
2020-07-30 16:37:282546browse

Analysis of the relationship between stack memory, heap memory and method area

Let’s take a look at the code first:

(Recommended tutorial: java introductory tutorial)

package cn.liang.jvm;
    public class SimpleHeap {	
            private int id;	
            public SimpleHeap(int id){		
                this.id = id;
	    }	
        public void show(){
	    System.out.println("My ID is " + id);
	}	
        public static void main(String[] args) {
		SimpleHeap s1 = new SimpleHeap(1);
		SimpleHeap s2 = new SimpleHeap(2);
		s1.show();
		s2.show();
	}
}

Relationship diagram:

Analysis of the relationship between stack memory, heap memory and method area

(Video tutorial recommendation: java video tutorial)

  • The SimpleHeap instance itself is allocated in the heap memory

  • The information describing the SimpleHeap class is stored in the method area

  • The s1 and s2 local variables in the main() function are stored in the Java stack memory and point to the heap Two instances of memory

Heap area:

1. All objects stored are objects, and each object contains information about a corresponding class. (The purpose of class is to get operation instructions)

2. JVM has only one heap area (heap) shared by all threads. Basic types and object references are not stored in the heap, only the object itself is stored.

Stack area:

1. Each thread contains a stack area. Only objects of basic data types and references to custom objects (not objects) are stored in the stack. Objects are stored in the heap. Zone

2. The data (original types and object references) in each stack are private and cannot be accessed by other stacks.

3. The stack is divided into 3 parts: basic type variable area, execution environment context, and operation instruction area (storage operation instructions).

Method area:

1. Also called the static area, like the heap, it is shared by all threads. The method area contains all class and static variables.

2. The method area contains elements that are always unique in the entire program, such as class and static variables.

The above is the detailed content of Analysis of the relationship between stack memory, heap memory and method area. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete