JVM principles _ memory

Source: Internet
Author: User
Tags xms


This article will introduce the working principles of JVM from the developer's perspective, and I will continue to update it. If you find something wrong, you are eager to give us some advice.




1. Introduction to JVM


JVM is short for Java Virtual Machine (Java Virtual Machine). Java language usage mode: Java Virtual Machine shields information related to specific platforms, so that the Java language compiler only needs to generate the target code (bytecode) that runs on the Java Virtual Machine, and can run it on multiple platforms without modification. When executing the bytecode, the Java Virtual Machine interprets the bytecode as a machine instruction execution on a specific platform. JVM also has its own specifications so that third parties can develop their own JVMs, such as IBMJVM, MSJVM, and hotspot jvm (SUN ). This article is based on the introduction of the hotspot jvm.


Ii. JVM Memory Structure


JVM memory framework





Note: The sequence number is marked from left to right. The sequence number does not represent the logical relationship.


Next we will introduce it based on the logical relationship.


1 JVM Process Heap

32-bit OS: up to 2 GB

64-bit OS: more.




2 Java Object Heap (JAVA Heap)

Generally known as JVM heap, it is easy to confuse with JVM process Heap. It is used to store java objects such:

The basic data and reference of the Object.


-XX: MinHeapFreeRatio =

Default is 40 (40%)

-XX: MaxHeapFreeRatio =

Default 70%


The initial memory allocated by JVM is specified by-Xms. The default value is 1/64 of the physical memory. The maximum memory allocated by JVM is specified by-Xmx. The default value is 1/4 of the physical memory. By default, when the free heap memory is smaller than 40%, the JVM will increase the heap size until the maximum limit of-Xmx. When the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-Xms. Therefore, the server generally sets-Xms and-Xmx to be equal to each other to avoid adjusting the heap size after each GC.




3. Young Generation

All newly created objects will be stored here.

Configuration method:

-Xmn-not preferred (fixed value)

-XX: NewRatio = <value>-preferred


The officially recommended configuration is that Young Generation accounts for 33% of the total Java Object Heap memory.

4. Eden Space

New objects are always created in Eden Space.


5. Old Generation


If Young Generation's data survive one or more GC operations, it will be transferred to OldGeneration.


6. Elastic vor Spaces


During GC, the active objects in Eden are copied to surivivor spaces. When the objects reach the minimum value (aging), they are sent to the Old Generation.

10. Tenured Space

5 MB min 44 MB max (default)


This is the legendary old age. The official explanation is:

Pool ining objects that have existed for some time in the specified vor space.



11. Everything else

This is what we often see as no-heap.



12. Permanent Space

4 MB initial, 63 MB max

The function that stores the class and its meta Data.

Configuration method:

-XX: PermSize = <value> (initial)

-XX: MaxPermSize = <value> (max)


13. Code Generation


Converts byte code to native code.

It basically does not cause memory exceptions.

If this operation does not have enough space, the JVM may crash.



14. Socket Buffers

It consists of two parts: 1: Receive buffer ~ 37 k 2: Send buffer ~ 25 k

It needs to be controlled in JAVA code, so it cannot be configured externally.


He may cause an exception: IOException: Too program open files (




15. Threaded JVM Stacks


Jvm stacks:


Jvm stack: frame data, operand stack...



Thread Stacks:

The space allocated by each Thread.

The default value depends on OS/JVM.

The number of threads increases, and Thread Stacks increases.



Exception: java. lang. OutOfMemoryError:

Unable to create new native thread

If the-Xss configuration is too small

Java. lang. StackOverflowError


16. Direct Memory Space


It allows developers to map memory out of java Object Heap.

Configuration: XX: MaxDirectMemorySize = <value>



17. JNI Code,


JNI code uses very small memory.


19. JNI allocate memory

The JNI program also needs to allocate memory.



18. Garbage Collection


GC also requires memory, gc thread consumption, and information stored in the GC cache.


Here is a brief introduction to GC history:

GC started with the 1959-LISP Language

His original intention:

1. Automatic Memory cleanup

2. Make Development easier

3. Make debugging easier


Gc Process

1. Lock it down)

All objects are locked during GC to ensure they do not change.

2. Mark)

Traverse all objects and Mark unreachable objects as garbage (need to be recycled)

3. Clean (Sweep)

Delete all marked objects

Memory cleanup


GC problems in earlier JAVA versions

1. GC cannot be well coordinated.

2. There is only one algorithm.

3. Mark and Sweep scan require a long time to scan the entire Heap. Of course, the time depends on the heap size. So people invented the Permanent Space)



GC Working principle:

Eden-all newly created objects are placed here.

Protected vor-when the Eden area has insufficient space, the surviving objects will be copied to one of the two protected vor areas (FromSpace and ToSpace). If there is insufficient space in this protected vor area, then, the surviving objects in the block area are copied to the other area. Note that there is always a region vor that is empty.

The garbage collection of Young Generation is called minor GC, and many objects cannot survive GC.


Old Generation -- when an existing vor region is full, objects in the region that have survived GC for a certain number of times are put in Old Generation. If the Old Generation is Full, Full GC is required. Full GC is very performance-consuming. When Full GC occurs, the application will be suspended. Because most objects cannot survive GC once, if Full GC occurs frequently on the server, you should check whether there is a problem.



Iii. Exceptions





Java. lang. OutOfMemoryError: Java heap space

Cause: Heap memory overflow means that the memory of Young and Old generation is insufficient.

Solution: Adjust the java startup parameter-Xms-Xmx to increase Heap memory.

Java. lang. OutOfMemoryError: unable to create new native thread

Cause: the Stack space is insufficient to create additional threads, either too many threads to be created or the Stack space is indeed small.

Solution: Because JVM does not provide parameters to set the total size of stack space, you can set the size of a single thread stack. The user space of the system is 3 GB in total, in addition to Text/Data/BSS/MemoryMapping segments, the total Heap and Stack space is limited. Therefore, this error can be solved in two ways: 1. use the-Xss startup parameter to reduce the size of a single thread stack so that more threads can be enabled (of course, StackOverflowError may occur if it is too small); 2. the-Xms-Xmx parameter is used to reduce the Heap size and give the memory to the Stack (provided that the Heap space is sufficient ).

Java. lang. OutOfMemoryError: PermGen space

Cause: the Permanent Generation space is insufficient and no additional classes can be loaded.

Solution: Adjust-XX: PermSize =-XX: MaxPermSize = two parameters to increase the PermGen memory. Generally, do not set these two parameters manually. You only need to set-Xmx to be large enough, and the JVM will select the appropriate PermGen size.

Java. lang. OutOfMemoryError: Requested array size exceeds VM limit

Cause: this error is rare (try to create an array with a length of 0.1 billion). It is also caused by insufficient Heap space. If a new array is needed, the program logic is mostly unreasonable.

Solution: modify the program logic. Alternatively, you can use-Xmx to increase the heap memory.

When GC takes a lot of time, but only a small amount of memory is recycled, an OutOfMemoryError is reported. I have encountered only one or two times. When the-XX: + UseParallelGC or-XX: + UseConcMarkSweepGC collector is used, an error is reported in the preceding case. The following is the description in the HotSpot GC Turning document:

The parallel (concurrent) collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown.

To solve this problem, perform GC turning and optimize the program logic.

Java. lang. StackOverflowError

Cause: this is also a kind of memory overflow error, that is, the thread stack overflow, either because there are too many methods calling layers (such as infinite recursive calls), or the thread stack is too small.

Solution: optimize the program design, reduce the method call level, and adjust the-Xss parameter to increase the thread stack size.


IOException: Too program open files

Cause: this is because the buffer size of TCP connections is insufficient.


Java. lang. OutOfMemoryError: Direct buffer memory


Solution: Adjust-XX: MaxDirectMemorySize = <value>

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.