Memory Management
Memory allocation and deallocation
Memory allocation is done by the program, memory release done by GC
Runtime data area
(1) Program counter (program counter register)
A smaller memory space
Bytecode instructions executed by the current thread the line number indicator, bytecode interpreter at work is to be selected by changing the value of the program counter to execute the next hop
Multi-threaded environment, thread switch turns executed, the program counter guarantee thread after switching can be restored to the correct location
Each thread has a separate program counter
Thread-private
There is no exception
Address of the virtual machine instruction bytecodes java method, the program counter is currently being executed
native method, program counter value is null (undefined)
(2) the virtual machine stack (stack)
Is a virtual machine stack Java memory model described method performed: Each method creates a stack frame in the process of implementation, local variable table for storing information, operand stack, dynamic links, and so on for export
Local variable table stores various types of data found during compilation: boolean byte char short int float long double object references (long and double take two local variable space slot)
Determining the size of the local variable table during compilation
Thread-private
StackOverflowError: a depth greater than the allowable stack virtual machine thread requests stack
OutOfMemoryError: Java Virtual Machine stack dynamic enough memory can not apply to the extended
(3) native method stacks
VM stack for the Java virtual machine to perform method Service
Native method stacks of virtual machines are Native method Service
(4) heap
Threads share (java heap)
Store all the instances of objects and arrays
The biggest piece of Java memory area
The main area (GC heap) (garbage collected) (Cenozoic years old) (Eden, from survivor, to survivor) garbage collection
Multi-threaded environment, thread-private allocated on the heap allocation of buffer (TLAB)
Heap memory may not be physically contiguous storage space, can be logically consecutive
-Xms: JVM initial allocation of heap memory size, the available memory for the operating system typically 1/64
-XMx: JVM heap maximum limit may be allocated, the operating system memory available 1/4
-Xms and -Xmx is set to be as large as possible to avoid after each garbage collection, JVM reallocate memory
The new generation of reactor = + + years old and lasting generations
OutOfMemoryError: Can not get enough memory heap memory expansion
(5) Method region
Threads share
Class information storage has been loaded in the virtual machine, constants, static variables, even after the code compiler
When the method area can not meet the needs of memory allocation: OutOfMemoryError
(6) runtime constant pool
Part of the method area
In addition to the class file versions described classes, fields, methods, interfaces, etc., there is a constant pool (constant pool table), and for a variety of literal symbols generated during compilation storage references
After storage class loader
In addition to saving Class symbol described in the file reference, but also to translate it directly references are also stored in the constant pool
OutOfMemoryError
Memory allocation and recovery strategies
(Can be determined after the memory occupied by the object class loading completion) to allocate an object
Memory recovery object (reachability analysis did not reach a successful self-help objects)
(1) priority in the allocation of Eden target area (if the local thread starts to allocate the buffer, press the thread priority assigned TLAB)
When there is not enough space for Eden district, occurred MinorGC
-Xms: Java heap size initialization
-Xmx: Java maximum heap memory size (the same as the initial and maximum size, jvm will not re-allocate memory after a garbage collection)
-Xmn: Java heap size of the new generation (new generation = Eden + 1 th survivor + another Survivor)
-XX: SurvivorRatio = 8 ratio of the new generation area size and Eden Survivor
(2) large objects directly into the old year
Large Object: an object requires a lot of contiguous memory space (array, a long string)
(3) long-lived objects into the old years
Generational collection
Target age counter
Objects allocated in Eden area, after after a Minor GC, still alive, and the size of the area can accommodate Survivor, Survivor into the area, the age is set to 1, the object is to get through each time zone in the survivor Minor GC age-plus-one, when over the age set value (default 15), will be promoted to the old year
-XX:MaxTenuringThreshold
(4) determine the age of dynamic objects
When an object reaches the age of MaxTenuringThreshold not always wait, it will be promoted to the object's old
If you take in Survivor, the total size of all the objects of the same age in the general area larger than the size of the survivor, all objects of this age and older are promoted to the old year
(5) space allocation guarantees
Between Minor GC is performed, old age checks the maximum available contiguous space is larger than the new generation of the total space of all objects
If true, explain the Minor GC must be safe.
If you do not set up, check the parameters HandlePromotionFailure, whether to allow space allocation guarantees failure
If allowed to fail, check the old age is greater than the maximum available space of the previous promotion to the average size of old age
If so, it will attempt Minor GC, despite the Minor GC risk
If not, a Full GC / Major GC
If allowed to fail, a Full GC / Major GC