0010 Java JVM Virtual Machine 7 block of memory Partition "Getting Started" __java

Source: Internet
Author: User

Jingle: Pool heap, stack of stacks

Discard the pool, station music (a left the bathroom, stomach pain stand up)

The memory management mechanism in Java is managed automatically through the JVM. Understanding the fundamentals of the JVM helps to understand Java more.

We first need to understand the allocation mechanism of Java memory, in the Java Virtual Machine specification, the JVM is divided into 7 memory regions.

The 7 memory areas in the virtual machine specification are three thread-private and four-thread-shared memory areas.

Thread-Private Memory Area: thread-private memory areas have the same lifecycle as threads, which are: instruction counters, line stacks, and local line stacks.

Four shared areas: four shared areas are shared by all threads and are allocated when the JVM is started, respectively: Method area, Chang, direct memory area, and heap (that is, the memory of our commonly called JVM is divided into heaps and stacks in the stack, which is the front line stacks).

1, instruction counter.

We all know that Java multithreading is run through the JVM to switch the time slice, so that each thread may or may not be suspended at some point, so when the thread hangs, how does the JVM know that the thread is going to run that byte-code instruction when it dispatches it again? This requires a memory area associated with the thread to record the next instruction for that thread, and the instruction counter is the area of memory that implements the function. How many threads are indeterminate at compile time, therefore there is no way for the zone to be allocated at compile time, only when the thread is created, so that the zone is thread-private, the area is only a count of instructions, and there is very little space to occupy, so the virtual machine specification does not outofmemoryerror the region.

2, line stacks (virtual machine stack).

The default size for each stack space is 0.5M, in 1.7 to adjust to 1M, each call method will be pressed into a stack frame, if the indentation of the stack frame depth is too large, that is, the method call level too deep, will throw stackoverflow,,sof the most common scenario is recursion, when recursion can not exit, The exception is thrown, hotspot provides parameter settings to resize the area, using-XSS:XXK, you can modify the default size.

3, the local line stacks.

This area is allocated primarily to the thread that invokes the local method. The biggest difference between the area and the line stacks is that the requested memory for that thread is not managed by the GC and requires the caller to manage it himself, most of the math classes in the JDK are local methods, and a noteworthy problem is that when the local method is executed, is not running bytecode, so the instruction counter is not able to record the next byte code instruction, when the local method is executed, the instruction counter is set to undefined.

The difference between the local method stack and the line stacks is that the line stacks performs Java method services for the virtual machine, and the local method stack is the native method service that is used by the virtual machine.

4, Method area.

This area is used to store class information for the JVM's loaded class, including: Class methods, static variables, type information (interfaces/parent classes), and the information we need to use the reflection technology is obtained from here.

In general, the memory recovery target for this area is the collection of constant pools and the unloading of types.

5, Constant pool.

The variable is final decorated and can no longer modify its value. So it becomes a constant, and the constant is stored in the constant area, which knows the size of the footprint at compile time, but it doesn't mean that the zone compiles and the runtime can modify the size of the constant pool, typically when you use string, You can call intern () of string, the JVM determines whether the currently created string object is in a constant pool and, if so, takes it from a constant, or places the character in a constant pool and returns, changing the size of the constant pool (the size of the constant is immutable).

6, direct memory area.

The direct memory area is not an area of memory that the JVM can manage. In the NIO provided in JDK1.4, an efficient r/w operation is achieved, and the efficient r/w operation is achieved through a piping mechanism that actually uses local memory, thus avoiding the process of replicating the JVM memory from the local source file and copying it from the JVM to the target file. Copied directly from the source file to the target file, the JVM operates direct memory through Directbytebuffer.

= =. A new NiO class is added to the JDK1.4, which introduces an I/O method based on channel and buffer, which can allocate the heap memory directly using the native function, and then operate as a reference to the memory by a Directbytebuffer object stored in the Java heap. This can significantly improve performance in some scenarios because it avoids replicating data back and forth in the Java heap and the native heap. To

7, Heap.

The heap is the most common in the JVM, the most familiar area of memory, what we normally call the GC is mostly done in this area, where all of the Java objects are allocated, and this is the largest memory area in the JVM, shared by all threads, where thousands of objects are created and destroyed here.

Object Access

In the Java language, how objects are accessed. Even the most common object access involves the three most important memory areas of the Java heap, the Java stack, and the method area, as shown in the following code:

Object obj = new Object ();

Assuming that the code appears in the method body, the semantics of the part "Object obj" will be reflected in the Java stack's local variable table, appearing as a reference type. The "New Object ()" Part of the semantics will be reflected in the Java heap, forming a piece of structured memory that stores all instance data of type Object. You must also include address information in the Java heap that can find data for this object type, such as Object types, parent classes, implemented interfaces, methods, and so on, which are stored in the method 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.