Java GC
Java Memory Model
![jvm-architecture]((https://github.com/LiMingFei56/picturebed/raw/main/jvm/jvm-architecture.png)
JVM参数:
- -XmsSetting — initial Heap size
- -XmxSetting — maximum Heap size
- -XX:NewSizeSetting — new generation heap size
- -XX:MaxNewSizeSetting — maximum New generation heap size
- -XX:MaxPermGenSetting — maximum size of Permanent generation
- -XX:SurvivorRatioSetting — new heap size ratios (e.g. if Young Gen size is 10m and memory switch is –XX:SurvivorRatio=2, then 5m will be reserved for Eden space and 2.5m each for both Survivor spaces, default value = 8)
- -XX:NewRatio — providing ratio of Old/New Gen sizes (default value = 2)
堆内存
- Heap is divided into 2 parts — Young Generation and Old Generation
- Heap is allocated when JVM starts up (Initial size: -Xms)
- Heap size increases/decreases while the application is running
- Maximum size: -Xmx
Young Generation
- This is reserved for containing newly-allocated objects
- Young Gen includes three parts — Eden Memory and two Survivor Memory spaces (S0, S1)
- Most of the newly-created objects goes Eden space.
- When Eden space is filled with objects, Minor GC (a.k.a. Young Collection) is performed and all the survivor objects are moved to one of the survivor spaces.
- Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of the survivor space is always empty.
- Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually it’s done by setting a threshold for the age of the young generation objects before they become eligible to promote to Old generation.
Old Generation
- This is reserved for containing long lived objects that could survive after many rounds of Minor GC
- When Old Gen space is full, Major GC (a.k.a. Old Collection) is performed (usually takes longer time)
Reference
Java (JVM) Memory Model - Memory Management in Java
Understanding Java Memory Model