Deep understanding of Java Virtual Machine (1)-java memory Area

Source: Internet
Author: User

This blog will provide a conceptual explanation of the various areas of Java Virtual Machine memory, the role of these areas, the service objects, and the problems that may arise, which is what we are prepared to do later on to learn about Java memory management.

Java Virtual machine in the process of executing Java program, it will be managed by the memory divided into a number of different data areas, these areas have their own purposes, as well as the creation and destruction of time, not to say more, directly to the image below, is the area. Maybe you've only heard about stacks, heaps, and constant areas, but these areas are just a few of the areas we use, and we'll make a brief introduction to each area.
1. Program Counter

This area should be unfamiliar to everyone, probably never heard of it. The program counter is a smaller memory space, and his role can be seen as the line number indicator of the byte code executed by the current thread, which is used to record where our program is executing, and to raise a chestnut: we know that many times our programs are multi-threaded programs, In the operation of the entire application, threads are constantly switching, thread a executes to method A to half, and thread B begins to execute method B, so when thread A gets the resources to start executing its own method, how does he know where he is going to start executing, so he introduces the program counter, It records where the byte code was executed before thread A, and the corresponding counter records where thread B executes. By this example you should know what the program counter is doing, and also can see that each thread needs to have a separate program counter, each thread counters do not affect each other, isolated storage, we call such areas as "thread-private" memory.
If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode directive, if the native method is being executed (Chinese name: Local method, explanation, because there is a local method stack behind it, the local method refers to those interfaces that invoke non-Java code implementations , it is an interface, its true implementation is implemented in other languages, there are many methods in the Java virtual machine is not implemented in the Java language, because some methods may be more efficient in other languages, we call such methods called local methods, this counter value is empty.
(Knocking on the blackboard) This memory area is the only area in the Java Virtual Machine specification that does not specify any outofmemoryerror conditions, and all areas you see later can be outofmemoryerror. 2.Java virtual Machine stack

The virtual machine stack describes the memory model executed by the Java method (closely related when the Java method executes): Each method is executed at the same time to create a stack frame, used to store the operand stack, dynamic link, method export, local variable table (this is what we usually say the stack, unexpected bar, It's just a local variable table in the Java Virtual machine stack. Each method is invoked until the completion of the process, corresponding to a stack frame in the virtual machine from the stack to the stacks of the process, so through this sentence, you should realize that the area should also be thread-private, because the method when a thread executes.
What you must know is that the local variables table holds the various basic data types known to the compiler, and references to various types of objects. The 64-bit length and double data occupy 2 local variable spaces, while the remaining data types occupy 1 local variable spaces.
Then we'll discuss the issue of memory overflow, and here's what we'll mention later, in the Java Virtual Machine specification, there are two exceptions to this area: if the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown , but a generic virtual machine can be dynamically extended, but it throws a OutOfMemoryError exception if it cannot be applied to enough memory at the time of the extension. 3. Local method Stack

I have briefly described what a local method is, the local method stack and the virtual machine stack play very similar, the difference is that the virtual machine stack is to perform Java method services for the virtual machine, while the local method is for the virtual machine to use the native method service, The local method stack area also throws Stackoverflowerror and OutOfMemoryError exceptions. must also be thread-private. 4.Java Heap

The Java heap is the largest chunk of memory managed by the Java Virtual machine, is created when the virtual machine is started, the only purpose of this memory area is to hold the object instance, where almost all object instances are allocated memory, followed by the Java Memory Recovery algorithm using a generational collection algorithm, So the Java heap can also be subdivided into: the new generation and the old age, in the subdivision is Eden Space, from Survivor space and to survivor space (know that there is this thing on the line, later will be detailed in each of the space) if the heap does not have memory completion instance allocation, And the heap cannot be extended, the OutOfMemoryError exception is thrown. Obviously this is the memory area where the brother threads are shared, because each of these objects is now being manipulated on the heap. 5. Method Area

The method area, like the Java heap, is an area of memory that is shared by each thread and is used to store data such as class information, constants, static variables that have been loaded by the Xu Xian machine, such as compiled code for Just-in-time compilers. Just remember what's inside the way canal. When the method area is unable to meet the memory allocation requirements, the OutOfMemoryError exception is thrown.

Basically the 5 areas on the diagram are covered, here only need to know what each area is stored on the line, after the virtual machine memory recycling and distribution will be detailed design to each area of knowledge points. There are 2 more areas that we might encounter later, and here's a little bit of mention. The
(1) Run constant-rate pool
is a part of the method area, primarily used to hold the literal and symbolic references that the compiler holds. Another important feature of the
run-time pool relative to the class file constant pool is the dynamic nature of the the Java language does not require constants to be generated only in the compiler, that is, if the contents of a constant pool in the pre-implanted class file are not allowed to enter the method runtime, the new constants may also be placed in the pool during the run , the more that this feature is exploited by developers is the Intern () method of the String class (the method, when executed, it determines if the object is in the string constant pool, returns the object if it is already present, and if it is not created, here Jdk6 and Jdk7 are a little different. Interested to understand this method)
(2) Direct Memory
Direct memory is not part of a virtual Run-time data area, nor is it a memory area defined in the Java Virtual Machine specification, but this part of memory is also used frequently. It can also cause outofmemoryerror anomalies to occur.
The NIO (New Input/output) class introduced in jdk1.4 introduces a channel-and buffer-based I/O method that allows direct allocation of heap memory using the native function library. It then operates as a reference to this memory through a Directbytebuffer object stored in the Java heap, which can significantly improve performance in some scenarios because it avoids replicating data back and forth in the Java heap and the native heap.
When a server administrator configures a virtual machine parameter, it typically sets the-XMX parameter information according to the world's memory, but often ignores direct memory, making the sum of each memory area larger than the physical memory limit, resulting in an OutOfMemoryError exception when the extension occurs. 6. Take a chestnut and review our memory area (object access)

Object obj = new Object ();
Assuming that this code is in the method body, when the thread executes to the code, what is an allocation form in memory, first object obj will be reflected in the Java stack's local variable table, as a reference type of data, and the new object () This part of the semantics will be reflected in the Java heap, form a structured memory that stores all instance data values of the Objcet type, in addition, the Java heap must also contain address information that can be traced to this object type data (such as Object type, parent class, implemented interface, method, etc.). These types of data are stored in the method area.

As mentioned earlier, pointing to an object by reference, but without mentioning the way in which the reference is directed to the object, there are two ways to mainstream now, one is the handle and the other is the direct pointer.
1. Handle type: The handle type is equivalent to find a mediation handle (the location in the heap in the handle pool) only, the reference store is the address of the handle, the handle and then store the object instance data and object type data address. So that the reference can find all the information about the object.
2. Direct pointer access, reference directly store the object address (instance data address), and then the instance data stored in the type data pointer, point to type data. This also completes the reference to all the information found on the object.
Each has its own advantages, the use of handle access to the most advantage is that the reference store is a stable handle address, when the object is moving only need to change the handle of the instance data pointer, and its own reference do not need to change. The advantage of direct pointer is speed, because avoid the intermediate link. in front of the introduction of each district, also put the reason that each area may generate memory overflow, so if someone asked why there is a memory overflow, should know how to answer, my understanding is that the first thing is that the virtual machine in the process of executing the program does not request enough memory will cause memory overflow, The further refinement is how each area is causing the memory to overflow.

This article also lays the groundwork for the next important area of Java virtual machines, "garbage collector and memory allocation policy," so to understand the next chapter you must first understand the Java memory area partition.

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.