Further understanding of Java memory Management-simulation process diagram

Source: Internet
Author: User

Further understanding of Java memory Management-simulation process diagram-Reproduced

The memory management of Java is divided into:

1, heap memory, 2, stack memory, 3, method area, 4, local method area

/* 1:方法区       方法区存放装载的类数据信息包括:       (1):基本信息:       1)每个类的全限定名        2)每个类的直接超类的全限定名(可约束类型转换)       3)该类是类还是接口       4)该类型的访问修饰符       5)直接超接口的全限定名的有序列表       (2):每个已装载类的详细信息:       1)运行时常量池:       存放该类型所用的一切常量(直接常量和对其它类型、字段、方法的符号引用),它们以数组形式通过索引被访问,是外部调用与类联系及类型对象化的桥梁。它是类文件(字节码)常量池的运行时表示。(还有一种静态常量池,在字节码文件中)。       2)字段信息:       类中声明的每一个字段的信息(名,类型,修饰符)。       3)方法信息:       类中声明的每一个方法的信息(名,返回类型,参数类型,修饰符,方法的字节码和异常表)。       4)静态变量       5)到类 classloader 的引用:即到该类的类装载器的引用。       6)到类 class 的引用:       虚拟机为每一个被装载的类型创建一个 class 实例, 用来代表这个被装载的类。  2:栈内存       Java栈内存以帧的形式存放本地方法的调用状态(包括方法调用的参数,局部变量,中间结果等)。每调用一个方法就将对应该方法的方法帧压入 Java 栈,成为当前方法帧。当调用结束(返回)时,就弹出该帧。        编译器将源代码编译成字节码(.class)时, 就已经将各种类型的方法的局部变量, 操作数栈大小确定并放在字节码中,随着类一并装载入方法区。当调用方法时,通过访问方法区中的类的信息,得到局部变量以及操作数栈的大小。       也就是说: 在方法中定义的一些基本类型的变量和对象的引用变量都在方法的栈内存中分配。 当在一段代码块定义一个变量时,Java 就在栈中为这个变量分配内存空间,当超过变量的作用域后,Java 会自动释放掉为该变量所分配的内存空间, 该内存空间可以立即被另作它用。  栈内存的构成:       Java 栈内存由局部变量区、操作数栈、帧数据区组成。       (1):局部变量区为一个以字为单位的数组,每个数组元素对应一个局部变量的值。调用方法时,将方法的局部变量组成一个数组,通过索引来访问。若为非静态方法,则加入一个隐含的引用参数 this,该参数指向调用这个方法的对象。而静态方法则没有this参数。因此,在静态方法里无法访问对象信息。       (2):操作数栈也是一个数组,但是通过栈操作来访问。所谓操作数是那些被指令操  作的数据。当需要对参数操作时如 a=b+c,就将即将被操作的参数压栈,如将 b 和 c 压栈,  然后由操作指令将它们弹出,并执行操作。虚拟机将操作数栈作为工作区。       (3):帧数据区处理常量池解析,异常处理等  3:堆内存       堆内存用来存放由 new 创建的对象和数组。在堆中分配的内存,由 Java 虚拟机的自动垃圾回收器来管理。       在堆中产生了一个数组或对象后,还可以在栈中定义一个特殊的变量, 让栈中这个变量的取值等于数组或对象在堆内存中的首地址,栈中的这个变量就成了数组或对象的引用变量。 
    引用变量就相当于是为数组或对象起的一个名称, 以后就可以在程序中使用栈中的引用变量来访问堆中的数组或对象。 
4:本地方法栈内存       与调用的本地方法的语言相关,如:调用的是一个c语言方法则为一个c栈。本地方法可以回调java方法。若有java方法调用本地方法,虚拟机就运行这个本地方法。       在虚拟机看来运行这个本地方法就是执行这个java方法,如果本地方法抛出异常,虚拟机就认为是这个java方法抛出异常。       Java通过Java本地接口JNI(Java Native Interface)来调用其它语言编写的程序,在Java里面用native修饰符来描述一个方法是本地方法。 */

Here's a simple code example that understands how memory is allocated and managed in Java. Examples are as follows:

 Public classJavaramexplain { Public Static voidMain (string[] args) {person.whatcountry (); Person Sam=NewPerson ("Sam", -);      Sam.introduceself (); }  }  classPerson {PrivateString name; Private intAge ;  Public StaticString Country =" China";  PublicPerson (String name,intAge ) {           This. Name =name;  This. Age =Age ; }         Public voidintroduceself () {System. out. println ("My name ' s"+ name +", I ' m"+ Age +"years old."); }         Public Static voidWhatcountry () {System. out. println ("This people come from"+country); }  } 

When you start running the Javaramexplain class, the JVM begins the allocation management of memory.

The plot process is:

(large picture, can not see the picture right-view image, to view the original)

You can see that after the eighth step of the work is finished:

The data in the stack memory is all released (so the life cycle of the data in the stack memory is known and fixed, because the stack memory is freed as the execution of the method ends);

The person object in the heap memory now has no object reference to it, so it will be treated as "garbage" in memory and waiting to be recycled. (The data in the heap memory is processed automatically by the Java garbage collection mechanism, so its life cycle is uncertain)

It can also be seen from the diagram:

The method of the class will be compressed and the stack, corresponding to the method used in the parameters (variables) will be stored in the stack memory and release. So stack memory is used to hold local variables (both basic and reference types).

The object of the class is stored in the heap memory, and the corresponding member variables contained in the object are stored in that space.

The class itself will be loaded into the in-memory method area when the JVM runs through the. class, as the name implies, the method area is used to hold the class's method code, where the normal method is stored in the non-static zone, and the static members (methods and variables) are stored in the dedicated static zone of the method area.

The stack memory is the running area of the method (because the local variables defined in the method need to open up space in the stack), the method area is the storage area of the method.

Further understanding of Java memory Management-simulation process diagram

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.