Java Virtual machine memory area partition and function explanation

Source: Internet
Author: User

Preface

Why sometimes learn to learn will suddenly feel all the degree is so boring, the man's every month also have a few days? Haha, otherwise is what, I still want to insist, can do less, but cannot what degree does not do. It's always over.

--wh

One, run-time data area

What is the run-time data area, you know, the focus of today is around this picture.

                

1. Program Counter (Register)

Byte code line number indicator executed by the current thread

Byte code interpreter work dependent counter control complete

By executing the line octave number record, let the thread rotate between each thread to switch between the counters do not affect each other

Thread private, life cycle is the same as thread, born with JVM startup, JVM shuts down and dies

When a thread executes a Java method, it logs its executing virtual machine bytecode instruction address

When the thread executes the Nativan method, the counter record is empty (Undefined)

The only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions

In this, many do not understand the matter, we learn too many threads, there are two threads, one of the threads can be paused, let other threads run, and then wait for their own CPU resources, and can start from the point where the pause, then why can remember the location of the pause, which relies on the program counter, through this example , about the function of the program counter.

2. Local Method Stack

Do not know that you have seen the source code did not, see should know, a lot of algorithms or a function of the implementation, are packaged into the local method of Java, the program directly by calling the local method on the line, the local method stack is used to store this method, the code to implement the function may be C or C + +, It's not necessarily Java implementation anyway.

The above two is not the focus of our study, the next three is the focus.

3. Virtual Machine Stack

This is something that everyone should know, and now let's talk about it, the virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store local variable tables, table of operations, dynamic connections, method exits, and so on, each method from the call to the completion of the process,    Corresponds to a stack frame in the virtual machine stack into the stack to the process. How do you understand this? For example, when executing a class (with the main method in the class), execute to the main method, create a stack frame for the main method, and then add to the virtual machine stack, the stack frame will hold the main method of the various local variables, object references and other things.

                    

When calling another method in the main method, there will be another method of the stack frame into the virtual machine stack, when the method call is finished, the stack, and then the main method is at the top of the stack, continue execution until the end, and then the main method stack frame stack, the program is over. In short, the virtual machine stack is a lot of stack frame into the stack, the stack frame in the city to store some variable names and so on, so we usually say that the stack is stored in some local variables, because the local variables are in the method. That is, in the stack frame, that's what it says.

The above mentioned three are threads are not shared, that is, this part of memory, each thread is unique, will not let other threads to access, the next two are thread sharing, there will be thread security issues.

4. Heap

A piece of memory area shared by all threads. The largest chunk of memory managed by a Java virtual machine, because the only purpose of the memory area is to hold the object instance. Almost all of the object instance degrees allocate memory here, which is usually what we call the new object, which creates a piece of memory in the heap to hold some of the information in the object, such as attributes and whatever. The heap is also the main area of garbage collector management. So many times it is called "gc Heap", and the garbage collection mechanism of virtual machine is explained in the next article. The majority of the degrees of local reference variables that are stored in the stack above the previous point are stored in the heap.

5. The method area and the run-time constant pool

As with heaps, it is the area of memory shared by each thread that stores the class information, constants, static variables, and compiler-compiled code (that is, stored bytecode files) that have been loaded by the virtual machine, as well as the constants in the method area, because there is a running constant pool in the method area. Why is it called run a constant pool, because in the post-compilation generation is a variety of literal (literal meaning is the value, such as int i=3, this 3 is the meaning of the literal) and the symbol reference, these are stored in a called Chang (this constant pool is in the bytecode file), when the class loaded into the method area, The contents of the constant pool are placed in the run-time pool. It is important to note that running constant pools and constant pools is not confusing, and the bytecode file also has a const pool, which is explained in detail in later chapters. Now you just need to know that there is a run-time constant pool in the method area, which is used to hold constants. Also, running a constant pool does not necessarily have to take constants from the byte-code constant pool, it is possible to put new constants into the pool during the run of the program, such as the String.intern () method, which is used to find out if there is a value in the run-time pool of the method area, and if so, Returns a reference to the value, and if not, the value is added to the run-time constant pool.

        

Second, practice. Draw a memory graph.

The most common analysis used is the heap, the virtual machine stack, and the method area.

For example: Look at the program below and draw the memory analysis diagram

  

The most important thing is to look at my analysis process, this figure because to show that the dynamic stack can not be painted, so only to draw it that way.

1, first run the program, Demo1_car.java will become demo1_car.class, add Demo1_car.class to the method area, check whether the byte code file constant pool has a constant value, if there is, then join the run constant pool

2, encounter the main method, create a stack frame, into the virtual machine stack, and then start running the program in the Main method

3, Car c1 = new car (); First encounter car this class, so the Car.java compiled into a car.class file, and then add the method area, the same as the first step. Then new Car (). Creates an area in the heap that holds the instance object created, with the address 0x001. There are two attribute values, color and num. The default value is null and 0

4, and then by C1 this reference variable to set the value of color and num,

5, call the Run method, and then create a stack frame, used to load the local variables in the Run method into the virtual machine stack, the Run method to print a word, after the end, the stack frame out of the virtual machine stack. And only the main method of this stack frame is left.

6, and then create a car object, so in the heap to open up a piece of memory, followed by the same steps as before.

So the analysis is over, and in the head there should be a general understanding of the heap, the virtual machine stack, and the method area. Notice the name of this method area, not just the method, can pack a lot of things.

This is just a simple analysis, can be more specific, 1, the creation of objects, in the heap to open memory when the memory is allocated? 2. How are object references found in our object instances in the heap? Through these two questions to deepen our understanding.

1. How do I allocate memory when I create an object and open up memory in the heap?

Two ways: pointer collisions and idle lists. Which of our specific uses depends on what we use in our virtual machines.

Pointer collisions: Assuming that the memory in the Java heap is absolutely regular, all used memory is put aside, free memory is placed on the other side, and a pointer is placed in the middle of the indicator as the demarcation point, the allocated memory is simply a pointer to the free space over there to move an example of the size of the object, this allocation scheme is called the pointer collision

Idle list: There is a list of which memory blocks in the record are useful, and a large enough space is found in the list to be allocated to the object instance, and then the records in the list are updated. This is called the free list.

2. How are object references found in our object instances in the heap?

This problem can also be called the object's access location problem, there are two ways. Handle access and direct pointer access. Draw two pictures to understand.

Handle access: A chunk of memory is partitioned in the Java heap as a handle pool, and the reference variable stores the handle address of the object, and the handle contains the individual address information for the object instance data and type data.

              

Explanation diagram: There is a reference variable in the stack that points to the address of a handle in the handle pool, which contains two addresses, an object instance data, and an object type data (in the method area because the class bytecode file is placed in the method area).

      

Direct pointer access: The reference variable is stored directly in the object address,

            

Explanation: In the heap there is no handle pool, pointing directly to the address of the object, which contains the address of the object type data.

Difference: Both of these have advantages,

The biggest benefit of using a handle to access is that the reference variable stores a stable handle address that changes the strength data pointer in the handle when the object is moved (which is a trivial behavior when the object is being garbage collected), but the address that the reference variable points to does not change.

The greatest benefit of using direct pointer access is faster, saving time spent on pointer positioning, but changing the address of the reference variable when the object is moved. In the example we analyzed above, the direct pointer access method is used.

Description of the partition and role of the Java Virtual Machine memory area

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.