JVM Series article (i): Java Memory Area analysis

Source: Internet
Author: User
Tags cas

As a programmer, it's not enough to just know how to use it. At the very least, you need to know why you can use it, which is what we call the bottom.

So what is the bottom? I don't think it generalize. At my current level of knowledge: For Web developers, TCP/IP, HTTP, and so on may be the underlying, for C, C + + programmers, memory, pointers and so on may be the underlying things. For Java developers, the JVM where your Java code runs is probably what you need to understand and understand.

I will spend the rest of my time learning about the JVM with the reader, all of which are referred to in-depth understanding of Java Virtual machines: JVM advanced Features and Best Practices (second edition), thanks to the author.


This article is the first article in a series of articles, which tells Java Memory Area, that is, how the data is stored on the virtual machine.
One, run-time data region
The run-time data area is divided into two parts, part of which is shared by all threads, and partly private by individual threads. A thread-shared data area includes a method area and a heap, and a thread-private data area includes a virtual machine stack, a local method stack, and a program counter. As shown in the following:

(Image from the Web library)

Here's a description of each of these areas:


1. Program counter

A small amount of memory space can be seen as the line number indicator of the byte code executed by the current thread.

If the thread is executing a Java method, the counter value is the address of the currently executing virtual machine bytecode instruction, and if the native method is being executed, the counter value is empty.

This memory area is the only area that will never appear outofmemoryerror.


2. Virtual Machine stack
The thread is private, and its life cycle is the same as the thread. Describes the memory model that the Java method executes: Each method creates a stack frame at execution time to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack to the process. Local Variables table: The local variable table holds the basic data type (8, Boolean, etc.) that is known at compile time, the object reference (a reference pointer to the start address of the object, or a handle to a representative object, or other location associated with the object), The ReturnAddress type (the address that points to the bytecode Directive). The memory space required for a local variable table is allocated at compile time, and when the method is entered, it is completely determined that the local variable space allocated for this method in the frame is fully deterministic and the runtime does not change.
A local variable table can have two exceptions: if the thread requests a stack depth greater than the virtual machine allows, throw a Stackoverflowerror exception, or if the virtual machine stack can be dynamically extended (most virtual machines can), if the extension cannot request enough memory, The OutOfMemory exception is thrown.
3. Local Method Stack
The effect is similar to a virtual machine stack, where the difference is that the virtual machine performs a Java method (that is, bytecode) service for the virtual machine, and the local method stack serves the native method used by the virtual machine. Stackoverflowerror and OutOfMemoryError exceptions are also thrown.
4.Java Heap
For most applications, the Java heap is the largest piece of memory managed by a Java virtual machine. The Java heap is shared by all threads and is created when the virtual machine is started. The only purpose of this memory area is to hold object instances where almost all object instances (and arrays) are allocated memory.
The Java heap is the main area of garbage collector management, so it is often a GC heap. The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous. When implemented, it can be either fixed-sized or extensible. A OutOfMemoryError exception will be thrown if there is no memory completion instance allocation in the heap, and the heap can no longer be expanded.
5. Method area
All threads are shared, storing data such as the class information that has been loaded by the virtual machine, constants, static variables, code compiled by the instant editor, and so on. Although this area is known as a "permanent generation", there is still memory recycling in this area, mainly for the recovery of constant pools and the unloading of types. The OutOfMemoryError exception is also thrown by the method area.
Run a constant pool: part of the method area. class files In addition to the class version, fields, methods, interfaces, and other descriptive information, there is a constant pool, used to hold the compilation period generated by the various literal and symbolic references, this part of the class loaded into the method area of the run-time constant pool.
6. Direct Memory
Direct memory is not part of the data area when the virtual runtime is running, but this part of memory is also used frequently and may cause outofmemoryerror exceptions, so put it together here.
The NIO (New Input/out) class is added to the JDK1.4, introducing a channel-and buffer-based I/O method that can be used to directly allocate out-of-heap memory using the native library. It then operates as a reference to this memory through a Directbytebuffer object stored in the Java heap. This improves performance in some scenarios and avoids copying data back and forth in the Java heap and the native heap.
By the local total memory and processor address space (such as the processor is 32-bit, the address you can access through the content is 2^32, that is, 4G, so you can match the maximum memory is 4G) limit, also throws OutOfMemoryError exception.


Ii. creation, layout and access of objects
Once we know what's in memory, we naturally want to learn more about the other details in virtual machine memory. For example, how to create, layout, and how to access it. We take the most popular hotspot virtual machine and the common memory area Java heap as an example to explore the whole process of object assignment, layout and access.
1. Object creation we create objects, of course, with the new directive. Virtual opportunity to the new directive, first check whether the parameter of this directive can locate the symbol reference of a class in the constant pool, and check whether the class that the reference represents has been loaded, parsed and initialized. That The first step is to check that the virtual machine loads the class you want to new, and if it does not, you must first perform the appropriate class loading process. (This will be explained in more detail in a future article)
the memory is then allocated for the new object. The size of the memory required for an object is fully determined after the class is loaded. There are two ways of allocating memory: Pointer Collision: If the memory in the Java heap is absolutely structured, the memory used is placed on one side, the idle is stored on the other side, and the middle pointer is the indicator of the dividing point, then allocating memory simply moves that pointer over the free space to the same distance as the object's size. Free list:If it is not structured, the virtual machine needs to maintain a list of which memory blocks are available, find a large enough space in the list to be allocated to the object instance, and update the records on the list.
In addition to dividing the available space, you need to consider Thread-safety issues when modifying pointers。 There may be situations in which memory is being allocated to object A, the pointer has not been modified, and object B allocates memory using the original pointer at the same time. There are two ways to solve this problem: Synchronize the actions of the allocated memory space:The atomicity of the update operation is ensured by cas+ failure Retry (what is Cas:compare-and-wwap. Its principle: I think the position V should contain the value A; If you include this value, place B in this position; otherwise, do not change the position, just tell me the current value of this position. can refer to this article: CAS principle analysis) The action of allocating memory is in a different space divided by the thread:Each thread pre-allocates a small chunk of memory in the Java heap, called the local thread allocation buffer (TLAB), which thread allocates memory, is allocated on its own tlab, and if Tlab runs out and assigns a new Tlab, the synchronization is locked.
after the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to a value of 0. If you use Tlab, you can also advance to tlab allocation. This step ensures that the instance fields of the object can be used directly in Java code without assigning an initial value, and the program can access the 0 values corresponding to the data types of these fields.
Next, make the necessary settings for the object. For example, this object is the instance of which class, how to find the metadata information of the class, the hash of the object, the age of the object's GC, etc. This information is stored in the object header. Finally executed according to the programmer's wishes to initialize.

2. Memory layout of objects
Divided into 3 areas: object header, instance data, align padding.
Object HeaderIncludes two pieces of information: The first part is used to store the object's own run-time data, such as hash, GC generational age, lock status flag, thread-held lock, biased thread ID, biased timestamp, etc. This portion of the data is 32bit and 64bit, respectively, in 32-bit and 64-bit virtual machines, called Mark Word. This part of the data is many, beyond the limits that so many can record, so it is designed as a non-fixed data structure to store as much information as possible in a very small amount of space. (depending on the flag bit, its data represents a different meaning.) The other part of the object header is the type pointer, which points to its class metadata (metadata is the data about the data) that the virtual machine uses to determine which class the object is an instance of.
instance DataPart is the valid information that the object is actually stored, as well as the various types of field content defined in the code. Whether it's inherited from a parent class or defined in a subclass, it needs to be logged.
Snap To fillDoes not necessarily exist, only plays the role of placeholder. The automatic memory management system of the hotspot requires that the object start address must be an integer multiple of 8 bytes, so when the object instance data part is not aligned, it needs to align the fill to complement it.

3. Object access to the current mainstream access methods are using a handle and a direct pointer two. (a handle can be understood as a channel for indirectly accessing an object.) )
case with handle:The Java heap divides a chunk of memory as a handle pool, and the reference in the stack points to the handle address of the object, which contains the specific address information of the object instance data and the type data.
(Image from the Web library)

When using a direct pointer:The object address is stored in the reference.
(Image from the Web library)


Both of these ways Advantages of each: Using Handle AccessThe biggest benefit is that the reference is stored in the StableThe handle address, when the object is moved (garbage collection when moving objects is very common behavior) only changes in the handle of the instance data pointer, reference itself does not need to be modified. Use Direct pointer AccessThe biggest benefit is Fast Speed, saving time overhead for pointer positioning.


Analysis of abnormal production situation
1.Java Heap Overflow
As long as the objects are constantly created and the GC is guaranteed to have reachable paths between the objects to prevent the garbage collection mechanism from clearing these objects, a memory overflow exception occurs when the number of objects reaches the capacity limit of the maximum heap (roots).
To resolve this exception, the heap dump snapshot analysis is typically done through the memory image analysis tool to determine whether the memory object is necessary (that is, memory leaks or memory overflows). If it is Memory Leaks, you can further use the tool to view the reference chain of the leaked object to the GC roots, and pinpoint the location of the leak code more accurately. If it is Memory Overflow, you can either size up the virtual machine heap parameters, or check the code to see if some object life cycles are too long.
2. Virtual machine stack and local method stack Overflow
A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the maximum allowed depth of the virtual machine stack. Throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space when it expands the stack.
Defines a large number of local variables, increases the length of the local variable table in this method frame, and then throws Stackoverflowerror when the maximum allowed depth of the stack is reached. In single-threaded cases, it is difficult to throw outofmemoryerror exceptions. As you reach the maximum depth of the stack, the memory space is generally not exhausted.
In the case of multithreading, new threads are constantly created, and new variables are constantly created in new threads, and OutOfMemoryError may be thrown.

3. Method area and run-time-constant pool overflow
String.intern () is a native method that is used to return a string object that represents the string in the pool if the string constant in the pool already contains a character equal to that of this string object. Otherwise, the string contained by this string object is added to the constant pool, and a reference to this string object is returned. In JDK1.6 and earlier versions, because the constant pool is allocated in a permanent generation, the OutOfMemoryError exception is thrown if it is continually intern. Using JDK1.7 will not be thrown.
Method Area overflow Situation: a class to be reclaimed by the garbage collector, the judgment condition is more harsh. In applications where a large number of classes are generated on a regular basis, special attention needs to be paid to the recycling status. For example, dynamic languages, large numbers of JSPs, or applications that generate JSP files dynamically (JSP needs to be compiled into Java classes The first time it runs), OSGi-based applications (even the same class file, loaded by different loaders, are considered different classes.) But I don't have much research on OSGi, I'll have time to learn later.

JVM Series article (i): Java Memory Area analysis

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.