search
HomeJavaJavagetting StartedAnalysis of the relationship between stack memory, heap memory and method area

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:博客园. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function