JVM Memory model

Source: Internet
Author: User

Java enables multiple tasks to be processed at the same time through multithreading, where all threads share the JVM memory area, main memory, each thread has its own working RAM, and when the thread interacts with the memory area, the data is copied from RAM to the working memory, which is then processed by the thread.

I. Composition of the JVM logical memory model

1.1 Program counter Programs Counter Register

The address of the bytecode instruction executed by the current thread. When the bytecode interpreter works, it relies on changing the value of the counter to read the next byte-code instruction that needs to be executed.

If the thread is executing a Java method, the value of the counter is the address of the executing bytecode directive, and if it is the native method, the value of this counter is null undefined.

In order to return to the correct execution location after thread switching, each thread has a separate program counter, each of which has no effect on each counter. --Thread-private memory.

The only memory area in the JVM specification that does not specify any outofmemoryerror conditions.

1.2 JVM stack Java Virtual machine Stacks

The thread is private, the same as the thread's life cycle.

The JVM stack is the memory model that the Java method executes: When each method is executed, a stack frame is created to store the local variable table, the action stack, the dynamic link, the method exit, and so on. Each method is invoked until the completion process, which corresponds to a stack frame from the stack to the stack in the JVM stack.

The "Local variables table" holds the basic data types and object references that are known during the compilation period, where 64-bit long and double-type data consumes 2 local variable space slots, while the remaining data types occupy only 1. The amount of memory space required for a local variable table is allocated during compilation, and when entering a method, the method needs to allocate much of the local variable space in the frame is fully deterministic and does not change the size of the variable table while the method is running.

The JVM stack specifies two exception conditions: 1. If the thread requests a stack depth greater than the JVM allows, throw a stackoverflowerror exception; 2. If the JVM stack can be dynamically extended, the OutOfMemoryError exception is thrown when the extension cannot request enough memory.

1.3 Local methods Stack Native method Stacks

The difference from the JVM stack is that the local method stack serves the native method used by the JVM. The JVM specification does not enforce the local method stack, and virtual machines are freely implemented, and some virtual machines combine the local method stack and the JVM stack directly, such as the Sun hotspot virtual machine.

Also throws two kinds of exceptions: Stackoverflowerror,outofmemoryerror.

1.4 Java Heap Java heaps

The heap is the largest piece of memory that the JVM manages. shared by all threads and created when the JVM is started. The sole purpose is to hold an object instance. The vast majority of object instances allocate memory here.

The Java heap is the primary area that the garbage collector manages, so it is also called the GC heap most of the time.

Now the collector basically uses the algorithm of generational recovery, so the Java heap is further subdivided into: the new generation and the old age.

According to the JVM specification, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, just like a disk. The implementations of the current mainstream virtual machines are extensible (via-XMX and-xms control).

Throws a OutOfMemoryError exception if there is no memory available in the heap and cannot be extended.

1.4 Methods Zone Method area

Each thread is shared. Used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the JVM.

The JVM specification has a very loose limit on this area, with the exception of a heap, where no contiguous memory is required, a fixed size can be selected, or extensible, and you can choose not to implement garbage collection.

The general garbage collection behavior in this area is less, the recovery target is mainly for the recovery of the constant pool and the type of unloading, the conditions are very harsh. However, recycling is still necessary, or it may cause a memory leak.

The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

1.5 Running a constant pool runtime Constant pool

is part of the method area.

class file, in addition to the class version, fields, methods, interfaces, and other descriptive information, there is also a constant pool contant pools Table, used to hold the compilation period generated by the various literal and symbolic references, which will be stored in the method area after the class load in the run constant pool.

The runtime pool is dynamic, and the Java language does not require constants to be generated at compile time, and new constants may be placed in the pool during runtime.

The run-time constant pool is part of the method area and is also limited by the memory of the method area. can also throw outofmemoryerror exceptions.

1.6 Direct Memory

is not part of the JVM Runtime data area, nor is it a memory area defined in the JVM specification. But it can also lead to the appearance of OutOfMemoryError anomalies.

JDK1.4 added NiO, which introduces an IO method based on channel channels and buffer buffers, which can be used to directly allocate out-of-heap memory using the native library, and then operate through a Directbytebuffer object stored in the Java heap as a reference to this memory. This avoids replicating data back and forth in the Java heap and the native heap, which can significantly improve performance in some scenarios.

The allocation of direct memory is not limited by the JVM, but is subject to the size of the native total memory and the processor addressing space. OutOfMemoryError exception.

Second, object access

Object obj = new Object ();

1) Object obj, which is reflected in the local variable table of the Java stack, appears as a reference type data.

2) New Object (), which is reflected in the Java heap, forms a piece of structured memory that stores all instance data values of type object. The length of this memory is not fixed, depending on the specific type and the layout of the object memory implemented by the JVM. The Java heap must also contain address information that can find data for this object type. Type data refers to information such as object types, parent classes, implemented interfaces, methods, and so on, which are stored in the method area. That is: The type data is in the method area, and the object is in the heap.

3) Locate the object according to the reference. Different virtual machines implement different ways of accessing objects, the main access methods are: using handles, direct pointers.

The handle access method divides a chunk of memory in the Java heap as a handle pool. The reference stores the handle address of the object, which contains the specific address information of the object instance data and the type data.

The direct pointer mode reference stores the object address directly. In this way, the specific address of the storage type data must be considered in the layout of the heap object.

The greatest benefit of a "contrast" handle is that a stable handle address is stored in the reference, and only the instance data pointers in the handle are changed when the object is moved, and the reference itself is not modified. The greatest benefit of direct pointers is the fast access speed, which saves the time overhead of a pointer positioning. For Sunhotspot, use a direct pointer method.

Third, garbage collection

3.1 Determining the Garbage object

"Reference Counting Method"--python

Add a reference counter to the object, and whenever there is a reference to it, the value of the counter is incremented by 1, and when the reference fails, the value of the counter is reduced by 1, and any object with counter 0 at any time is impossible to be used.

The implementation is simple and efficient, but cannot solve the problem of circular references. (There may be two objects that refer to each other, but no third party points to either of them, and ultimately their reference count is not 0, they are never recycled)

"Accessibility Analysis Method"--java

Searches with the "GC Roots" object as the starting point. Starting down from these nodes, search all traversed paths as reference chains, proving that this object is not available when an object goes to GC roots without any reference chain.

An unreachable object is not necessarily a recyclable object. --Two times mark

Objects that can be used as GC roots include: 1) objects referenced in the JVM stack (local variable table), 2) objects referenced by class static properties in the method area, 3) objects referenced by constants in the method area, and 4) objects that are referenced by JNI in the local method stack.

"Reference type"

Strong references: Universally available, such as Object obj = new Object (), the object is never recycled as long as the strong reference is present.

Soft references: Some useful, but not necessary, objects. Before the system is about to have a memory exception, these objects will be listed in the Reclaim scope for a second collection, and a memory exception will be thrown if the second collection or insufficient memory is available.

Weak references: non-required objects. When the garbage collector is working, objects that are associated only with weak references are reclaimed regardless of whether the current memory release is sufficient.

Virtual reference: Ghost Reference or Phantom reference. A virtual reference does not affect the lifetime of an object at all, nor can it obtain an object instance through a virtual reference. The sole purpose of setting a virtual reference association on an object is to be able to receive a system notification when the object is reclaimed by the collector

Four, garbage collection algorithm

4.1 Mark-Clear algorithm Mark-sweep

Resources

http://blog.csdn.net/jiangwei0910410003/article/details/40709457

Resources

Http://www.cnblogs.com/dingyingsi/p/3760447.html

Http://www.cnblogs.com/dolphin0520/p/3783345.html

JVM Memory model

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.