JVM Memory Distribution

Source: Internet
Author: User

Reference article Address:

Http://hllvm.group.iteye.com/group/wiki/3053-JVM
http://blog.csdn.net/william001zs/article/details/6749946


Recommended article: http://www.cnblogs.com/gw811/archive/2012/10/18/2730117.html#undefined

1. When a new thread is created, the JVM creates a dedicated stack (stack) for each thread, and its stack is an advanced data structure, a way that allows programmers to program with particular attention to recursive methods

To use as little as possible, the size of the stack also has a certain limit, if too much recursion, easy to lead to stack overflow.

2. The JVM architecture consists of several major subsystems and memory areas:

1). class mount subsystem, which is responsible for loading the class from the file system into memory

2). GC subsystem, the garbage collector's main studio automatically recycles the memory that the program that is no longer running references the object, and it may also be responsible for those objects that are still in use to reduce heap fragmentation.

3). Memory area, used to store bytecode, objects created when the program is run, parameters passed to methods, return values, local variables, and intermediate calculation results.

3. memory allocation in Java

During the Java program run, the JVM defines various zones for storing run-time data. Some of these data regions are created when the JVM is started and are destroyed only when the JVM exits. Other data regions are associated with each thread

Related. These data regions are created when the thread is created and destroyed when the thread exits.

1). Program Counter Register (the PC Register)

The JVM supports multiple threads running concurrently. Each JVM has its own program counter. At any point, each JVM thread executes the code of a single method, which is the current method of the thread. If the method is not

native, the program counter register contains the address of the currently executing JVM directive, if The method is native, and the value of the program counter register is not defined. The width of the JVM's program counter registers is sufficient

enough to hold a return back to the address or native pointer.

In General, the program counter registers a small piece of memory, and its function can be seen as the line number indicator of the byte code that the current thread lock executes. At any one time, a processor executes only the instructions in one thread,

after the thread is switched back to the correct execution location, each thread needs a separate the program counter.

2). Stack

a). Stacks and Threads

The JVM is a stack-based virtual machine. The JVM allocates a stack for each newly created thread. In other words, for a Java program, it runs is done through the operation of the stack. Stack saves lines in frames

the state of the process. The JVM performs only two operations on the stack: stack and stack operations in frames.

we know that the method that a thread is executing is called the current method of this thread. We may not know that the frame used by the current method is called the current frame. When a thread activates a Java method, the JVM will be threaded to the Java stack

A frame is pressed into the new one. This frame naturally becomes the current frame. during the execution of this method, this frame is used to hold parameters, local variables, intermediate calculations, and other data. This frame here and the compilation principle of the activity record

The concept is similar.

from this allocation mechanism in Java, the stack can be understood as: stack is the storage area that the operating system establishes for a process or thread ( a thread in a multi-threaded operating system) for this thread .

The area has advanced post-out characteristics.

b). method calls in the stack, the stack of nested methods, and the stack:


The memory allocation graph of the stack is described in the nested method, as can be known from above, when nested method calls, the deeper the nesting, the stack's memory is released later, so the actual

in the development process, we do not recommend the use of recursive return method calls, recursion is very easy to cause stack flow.

The stack-up process for non-nested methods:



c). As with the program counter, it is also thread-private, responsible for almost local variables table, Operation Stack, dynamic link, method exit and other information, each method is called until the completion of the process, the corresponding

a stack frame in the virtual machine stack into the stack and the process of the stack (also known as line stacks, line The size of the stacks allocation can be configured in the JVM startup parameters-xss, when using the virtual machine default parameters, the stack depth

in most cases to achieve 1000-2000 completely no problem, the size of the line stacks, the system can generate a corresponding reduction in the number of threads, when the thread request the stack depth more than the virtual machine allows.

depth, the Stackoverflowerror exception will be thrown, and if the virtual machine dynamically expands, it cannot request enough memory, the OutOfMemoryError exception is thrown.

3). Heap

a). Each Java application uniquely corresponds to a single JVM instance, with each instance uniquely corresponding to a heap. All instances or arrays of classes created by the application in the run are placed in this heap and are applied by all

threads are shared. Unlike C + +, allocating heap memory in Java is automatically initialized. the storage space for objects in Java is allocated in the heap, but the reference to the object is allocated on the stack,

This means that when an object is created, the two Each place allocates memory, the memory allocated in the heap actually establishes this object, and the memory allocated on the stack is just a pointer to the heap object Needle (reference) only.

b). Memory dedicated to Java objects is the main area of garbage collector management, also known as the GC heap, which is divided into the new generation and the old age, the size of which is determined by the JVM startup parameters,

For example, the –xms 128m–xmx 512m indicates an initial size of 128m, which can be dynamically scaled up to 512m when the memory is low.

4). Method area

A ). The JVM has a method area that is shared by all threads. A method area is similar to a store of a traditional language's compiled code, or a text segment in a UNIX process. It stores each class structure such as Chang (constant pool),

member field fields and methods and constructors, including class and instance initialization and interface types The code for the special method used in the type. The method area is created when the virtual machine starts. Although the method area is logically part of the heap,

a simple implementation you can still choose to neither recycle nor compress it.

b). Stores data such as class information, constants, static variables, and immediately compiled code that have been loaded by the virtual machine, called non-heap (not heaps), or "permanent generations" (not necessarily "permanent" ),

It may also be recycled by the garbage collector, which can be-xx:permsize by the virtual machine and-xx:maxpermsize to limit the size.

5). Constant pool

Is part of a method area that holds various literal and symbolic references generated at compile time, such as some constants that are generated at run time.

Chang is used in Java to hold a copy of the compiled class file that has been determined during the compilation period. It includes constants in classes, methods, interfaces , and so on, including string constants, such as String s = "java" .

Of course, it can also be expanded, and the constants generated by the actuator will be put into constant pool, it is considered that Chang is a special memory space of the JVM. (Baidu Encyclopedia)

A string pool is part of a constant pool.

6). Direct Memory

memory that is not part of the Java Virtual Machine management, typically allocated memory space when using NIO, can be specified by-xx:maxdirectmemorysize, if not specified, the size of the-XMX is used by default. If the virtual machine

the memory is opened too large, will lead to the lack of direct memory, and lead to OutOfMemoryError error, you need to lower the memory footprint of the Java Virtual machine

4. Stack and heap comparisons:

The advantage of the stack is that the access speed is faster than the heap , second only to the registers directly in the CPU. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. In addition, the stack data can be shared. Heap of

The advantage is that the memory size can be dynamically allocated to survive you don't have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

5. Some classifications also have a local stack that is used to store local method (native) runtime data.

JVM Memory Distribution

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.