Drill down into the runtime data area of a Java virtual machine

Source: Internet
Author: User

Objective
Recently chewing a book, "Deep Java Virtual Machine", this book is not the first time to see, it can be said from the university began to look at,
This time should be the third time to chew the book, it should be said to be the first real bite of the book. College, just curious about the surface of some magical phenomena, with the work of several years later, now back to chew the book, for the surface of those who previously felt that the magic of the present has been feeling boring, but the bottom is how to achieve, how to operate more and more fascinated. This is also the original intention of this book to read. By writing a blog to record their own learning process, but also easy to look back to see what the current ideas will become in the future. If I have a misunderstanding or mistake in the following text expression or understanding, please the great God can leave a message, if there are classmates like me, also welcome message discussion, learning from each other.

Overview

       对于c/c++开发者而言,他们对内存管理拥有最高权利,但是需要干最多“活”的劳动人民。他们担负者每一个对象生命周期从开始到结束的责任(需要分配内存,回收内存)。对于java开发者而言,这些苦逼的活不需要做,因为都交给JVM的**自动内存管理机制**去做了。有句调侃的话叫:人生苦短,我用XXXX.那么java开发者真就轻松了吗?不!虽然把内存管理交给JVM去做了,但要是程序一旦出先内存泄漏、内存溢出等问题,如果不清楚虚拟机是如何使用内存的,那么排除错误将会比他们更加苦逼。

Run-time Data area

java虚拟机在执行程序过程中,会把**管理的内存**,划分为多个**数据区**。它们都有特定的用途。包括:方法区,堆,程序计数器,虚拟机栈,本地方法栈,运行时常量池,直接内存

1. Program counter

它是一块很小的内存。作用可看作是:当前线程所执行的字节码的行号指示器。字节码解释器就是通过它获取一条需要执行的字节码指令。java虚拟机多线程是通过线程轮流切换并分配处理器时间来实现的,在确定的时间一个处理器,只会执行一条指令。所以为了线程在切换时能够回到在正确的位置,每一个线程都有独立的程序计数器,是当前线程私有的。各个线程之间的计数器是互不影响,独立存储的,称之为线程私有的内存区域。    特点:        线程间互不影响,独立存储。

2. Virtual Machine stack

它也是线程私有的,生命周期与线程相同,是java方法执行时的内存模型。每个方法执行时,都会创建一个栈幀,用于存储局部变量表、操作栈、动态链接、方法出口等信息。局部变量表存放了编译期可知的基本数据类型,对象引用和return Address类型。

3. Local Method Stack

为虚拟机使用native方法服务。它的作用和虚拟机栈非常相似,不同点就是服务的对象不同,前者是给java方法服务,而后者是给native方法服务。

4.java Heap

是java虚拟机中管理最大的一块内存区域。是被所有线程所共享的内存区域。在虚拟机启动时创建,唯一目的就是存储对象实例。是java垃圾收集器的主要管理区域,这里又被成为GC堆。从内存回收来看,现在的垃圾收集器实现大都是使用分代收集算法,所以java堆还可以细分为:新生代,老年代,再细分为:eden空间,from servivor空间,To Servivor空间。该片内存,可以在物理上不是连续的,只要逻辑上连续即可。可动态扩展的(-Xms,-Xmx)。

5. Method area

所有线程共享,用于存储虚拟机加载的类信息、常量、静态变量、即使编译的代码等数据。运行时常量池是方法区的一部分。class文件内除了有类的版本信息、字段、方法、接口等描述信息外,还有一项常量池。这个常量池用于存放编译期生成的各种字面量和符号引用,在类加载后存放到方法区。

6. Direct Memory

并不是运行时数据区内存中的一部分,也不是虚拟机规范定义的内存,因为频繁使用,也可能出现OutOfMemoryError异常。在jdk1.4加入的NIO类,引入了一种基于通道/缓冲区的I/O方式,它可以使用native函数库直接分配堆外内存,通过存储在java堆中的DirectByteBuffer对象作为这块内存的引用进行操作。

Icon:

总结:    1.程序计数器,是线程私有的,线程间相互独立,是唯一一个不会抛出OutOfMemoryError异常的运行时数据区。    2.虚拟机栈,是线程私有的,与线程生命周期相同,是方法执行时的内存模型,通常说的栈内存指的就是这个    虚拟机栈。    3.本地方法栈,与虚拟机栈非常相似,服务对象不同,一个是服务Native方法,一个是服务Java方法。    4.Java堆,最大的一块内存,线程共享区,虚拟机启动时创建,垃圾收集器的主要用武之地,所以这里又叫    GC堆。可动态扩展。    5.方法区,所有线程共享,记录虚拟机加载的类信息,常量、静态变量、即时编译的代码。

Now that you know the partitioning of the runtime data area, it's time to start talking about how Java accesses objects.

    • Object access

Object access is ubiquitous in Java, so do you know how it implements access to objects? In Java, even the simplest object access is bound to
The three most important memory areas of the Java stack, Java heap, and method area are covered. Examples are as follows:

Object obj = new Object();

Assuming that the above statement appears in a method body , Object obj is reflected in the Java stack as a reference type data occurrence.
The new object () is reflected in the Java heap, in the Java heap, creating a piece of structured memory that stores instance data values of type object, in the Java heap
You must also save the address information for the object type data , and the object type data is stored in the method area .

But because the reference type refers to a reference to an object in the Java Virtual Machine specification, it does not describe how the reference is located and how to access the object's location in the Java heap. Depending on the virtual machine implementation, there will be different ways, the main way is two: handle, direct pointer
Here are two ways to do this:


Drill down into the runtime data area of a Java virtual machine

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.