[ Web Proxy ]
URL:
Viewing:
https://raw.githubusercontent.com/sharpener-YuFeng/JavaGuide/patch-2/docs/java/jvm/memory-area.md
[Back]
[Original]
--- title: Java description: JVMJava category: Java tag: - JVM head: - - meta - name: keywords content: JVM,,,,,,,Java --- > HotSpot > > Java JVM > > > > - Java > - Java > - ## Java C/C++ new delete/free Java Java ## Java Java JDK 1.8 JDK 1.7 JDK 1.8 **JDK 1.7**  **JDK 1.8**  **** - - - **** - - Java Java ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef feature fill:#00838F,color:#fff,rx:10,ry:10; classDef function fill:#4CA497,color:#fff,rx:10,ry:10; classDef state fill:#E99151,color:#fff,rx:10,ry:10; classDef lifecycle fill:#E4C189,color:#333,rx:10,ry:10; classDef warning fill:#C44545,color:#fff,rx:10,ry:10; %% Root(JVM ):::main %% 1 Root --> Attr[]:::feature Attr --> Attr1[/]:::feature Attr --> Attr2[]:::feature %% 2 Root --> Func[]:::function Func --> Func1[: //]:::function Func --> Func2[: ]:::function %% 3 Root --> Run[]:::state Run --> Run1[Java: ]:::state Run --> Run2[Native: Undefined]:::state %% 4 Root --> Life[]:::lifecycle Life --> Life1[/]:::lifecycle Life --> Life2[ OutOfMemoryError ]:::warning %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` - - - **** - **** **Java ** native ** JVM ** **native ** **Undefined** JVM JVM `OutOfMemoryError` ### Java ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root(
Java Stack):::main %% 1 Root --> Comp[]:::compare Comp --> Comp1[/]:::compare Comp --> Comp2[: Java ]:::compare Comp --> Comp3[]:::compare %% 2 Root --> Struct[]:::structure Struct --> S1[]:::structure %% 3 Root --> Err[]:::error Err --> Err1[StackOverflowError: ]:::error Err --> Err2[OutOfMemoryError: ]:::error %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` Java JVM Native Java  **** booleanbytecharshortintfloatlongdoublereference  **** ****  Java `StackOverflowError` **Java ** - **** return - **** **** `StackOverflowError` `OutOfMemoryError` `OutOfMemoryError` - **`StackOverflowError`** JVM `StackOverflowError` - **`OutOfMemoryError`** `OutOfMemoryError`  ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef implement fill:#E99151,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root():::main %% 1 Root --> Comp[]:::compare Comp --> Comp1[]:::compare Comp --> Comp2[: Native ]:::compare %% 2HotSpot Root --> Imp[]:::implement Imp --> Imp1[HotSpot ]:::implement %% 3 Root --> Struct[]:::structure Struct --> S1[]:::structure %% 4 Root --> Err[]:::error Err --> Err1[StackOverflowError: ]:::error Err --> Err2[OutOfMemoryError: ]:::error %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` ** Java Native ** HotSpot Java C Java JVM `StackOverflowError` `OutOfMemoryError` ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef implement fill:#E99151,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root(Java ):::main %% 1 Root --> Def[]:::compare Def --> Def1[JVM ]:::compare Def --> Def2[]:::compare Def --> Def3[]:::compare %% 2 Root --> Use[]:::structure Use --> Use1[]:::structure Use --> Use2[]:::structure Use --> Use3[]:::structure %% 3 (GC ) Root --> GC[]:::implement GC --> GC1[Eden + Survivor ]:::implement GC --> GC2[Old Generation]:::implement GC --> GC3[]:::implement %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` Java Java **** Java JIT HotSpot Java Java **GC Garbage Collected Heap** Java EdenSurvivorOld JDK 7 HotSpot GC Java 1. (Young Generation) 2. (Old Generation) 3. (Permanent Generation) Eden Survivor S0 S1  **JDK 8 PermGen Metaspace ** Eden S0 S1 1Eden ->Survivor 1 `-XX:MaxTenuringThreshold` 15 4 15 ```bash MaxTenuringThreshold of 20 is invalid; must be between 0 and 15 ``` ** 0-15?** 4 4 1111 15 0 15 HotSpot 3 HeaderInstance DataPaddingMark WordKlass Word HotSpot `markOop.hpp` Mark Word JDK  4 > ** [issue552](https://github.com/Snailclimb/JavaGuide/issues/552)**Hotspot Survivor `MaxTenuringThreshold` > > **** > > ```c++ > uint ageTable::compute_tenuring_threshold(size_t survivor_capacity) { > //survivor_capacitysurvivor > size_t desired_survivor_size = (size_t)((((double) survivor_capacity)*TargetSurvivorRatio)/100);//TargetSurvivorRatio 50 > size_t total = 0; > uint age = 1; > while (age < table_size) { > total += sizes[age];//sizes > if (total > desired_survivor_size) break; > age++; > } > uint result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold; > ... > } > ``` `OutOfMemoryError` 1. **`java.lang.OutOfMemoryError: GC Overhead Limit Exceeded`** JVM 2. **`java.lang.OutOfMemoryError: Java heap space`** :, , ( `-Xmx` [Default Java 8 max heap size](https://stackoverflow.com/questions/28272923/default-xmxsize-in-java-8-max-heap-size)) 3. ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef implement fill:#E99151,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root():::main %% 1 Root --> Def[]:::compare Def --> Def1[]:::compare Def --> Def2[JVM ]:::compare Def --> Def3[]:::compare %% 2 Root --> Store[]:::structure Store --> Store1[: //]:::structure Store --> Store2[: ]:::structure Store --> Store3[: ]:::structure %% 3HotSpot (JDK 7+) Root --> Change[]:::implement Change --> Change1[: Java JDK 7]:::implement Change --> Change2[: Java JDK 7]:::implement Change --> Change3[JIT : Code Cache ]:::implement %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` JVM Java Class **** 1. **** 2. **** 3. **** Class HotSpot - **Static Variables** JDK 7 `java.lang.Class` Java - **String Pool** JDK 7 ** Java ** - **JIT Code Cache**JIT **Code Cache**  **** Java HotSpot JDK 1.8 JDK 1.8  ** (PermGen) (MetaSpace) ?** Java 3 2.2.5  1 `-XX:MaxPermSize` `-XX:MaxMetaspaceSize` > `java.lang.OutOfMemoryError: Metaspace` `-XX:MaxMetaspaceSize` `-XX:MetaspaceSize` GC JVM 2 `MaxPermSize` , 3 JDK8 HotSpot JRockit , JRockit , 4 GC **** JDK 1.8 ```java -XX:PermSize=N // () -XX:MaxPermSize=N // () , OutOfMemoryError :java.lang.OutOfMemoryError: PermGen ``` JDK 1.8 HotSpot JDK1.7 ```java -XX:MetaspaceSize=N // GC -XX:MaxMetaspaceSize=N // Metaspace ``` `MaxMetaspaceSize` ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef implement fill:#E99151,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root():::main %% 1 Root --> Source[]:::compare Source --> Source1[ Class ]:::compare Source --> Source2[]:::compare Source --> Source3[]:::compare %% 2 Root --> Content[]:::structure Content --> Content1[: /]:::structure Content --> Content2[: //]:::structure %% 3 Root --> Error[]:::error Error --> Error2[ OutOfMemoryError]:::error %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` Class LiteralSymbolic Reference **(Constant Pool Table)** Java 7.34  `OutOfMemoryError` ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef implement fill:#E99151,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root():::main %% 1 Root --> History[]:::compare History --> Hist1[JDK 1.6: PermGen]:::compare History --> Hist2[JDK 1.7+: Heap ]:::compare History --> Hist3[: OOM GC]:::compare %% 2 Root --> Impl[]:::structure Impl --> Impl1[ HotSpot: ConcurrentHashTable]:::structure Impl --> Impl2[]:::structure Impl --> Impl3[]:::structure %% 3 Root --> Tuning[]:::error Tuning --> Risk1[StringTable Hash ]:::error Tuning --> Risk2[ intern ]:::error Tuning --> Param[-XX:StringTableSize ]:::error %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` **** JVM String ```java // 1. "ab""ab" // 2. "ab" aa String aa = "ab"; // "ab" bb String bb = "ab"; System.out.println(aa==bb); // true ``` HotSpot `src/hotspot/share/classfile/stringTable.cpp` HotSpot `StringTable` `-XX:StringTableSize` + HotSpot JDK1.7 JDK1.7 Java   **JDK 1.7 ** GC (Full GC) GCJava [JVM - RednaxelaFX - ](https://www.zhihu.com/question/57109429/answer/151717241) [ Java 3 &](https://github.com/fenixsoft/jvm_book) GitHub [issue#112](https://github.com/fenixsoft/jvm_book/issues/112) > **MetaspaceHeap ** ### ```mermaid graph LR %% classDef main fill:#005D7B,color:#fff,rx:10,ry:10; classDef compare fill:#00838F,color:#fff,rx:10,ry:10; classDef structure fill:#4CA497,color:#fff,rx:10,ry:10; classDef implement fill:#E99151,color:#fff,rx:10,ry:10; classDef error fill:#C44545,color:#fff,rx:10,ry:10; %% Root():::main %% 1 Root --> Source[]:::compare Source --> Source1[]:::compare Source --> Source2[ JVM ]:::compare Source --> Source3[]:::compare %% 2 Root --> Advantage[]:::implement Advantage --> Adv1[ I/O ]:::implement Advantage --> Adv2[ I/O ]:::implement Advantage --> Adv3[]:::implement %% 3 Root --> Error[]:::error Error --> Error1[ Java ]:::error Error --> Error2[]:::error Error --> Error3[ OutOfMemoryError]:::error %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` Java JNI `OutOfMemoryError` JDK1.4 **NIONew I/O****Channel****Buffer** I/O Java Java `DirectByteBuffer` JVM I/ONIO Selector I/O NIO Non-Blocking I/O Java NIO [`-XX:MaxDirectMemorySize`](https://docs.oracle.com/en/java/javase/25/docs/specs/man/java.html#extra-options-for-java) **** Java JVM JDK OpenJDK/HotSpot `DirectByteBuffer` Java `Cleaner` Java `OutOfMemoryError` ## HotSpot HotSpot Java ### Java ```mermaid graph TD %% classDef root fill:#004D61,color:#fff,rx:10,ry:10; classDef step fill:#005D7B,color:#fff,rx:10,ry:10; classDef detail fill:#4CA497,color:#fff,rx:10,ry:10; classDef logic fill:#E99151,color:#fff,rx:10,ry:10; %% Start(new ):::root Start --> S1[Step 1: ]:::step S1 --> S1_1[]:::detail S1_1 --> S1_2[//]:::detail S1_2 --> S2[Step 2: ]:::step S2 --> S2_Method{}:::logic S2_Method -->|| S2_A[]:::logic S2_Method -->|| S2_B[]:::logic S2_A & S2_B --> S2_Safe[: TLAB CAS ]:::detail S2_Safe --> S3[Step 3: ]:::step S3 --> S3_1[ 0]:::detail S3_1 --> S3_2[]:::detail S3_2 --> S4[Step 4: ]:::step S4 --> S4_1[Mark Word: /GC/]:::detail S4_1 --> S4_2[Klass Pointer: ]:::detail S4_2 --> S5[Step 5: init ]:::step S5 --> S5_1[]:::detail S5_1 --> S5_2[]:::detail S5_2 --> End(()):::root %% linkStyle default stroke:#005D7B,stroke-width:1.5px,opacity:0.8 ``` #### Step1: new #### Step2: ******** Java **** **** **** ** Java Java ** **** - - - - GC Serial, ParNew - - - - GC CMS Java Java GC --- **** - **CAS+** CAS ** CAS ** - **TLAB** Eden JVM TLAB TLAB TLAB CAS #### Step3: Java #### Step4: **** GC **** JDK JDK 15 #### Step5: init Java `` new `` ### Hotspot 3 **Header****Instance Data****Padding** 1. Mark WordHashCodeGC ID HotSpot 2. Klass pointer **** **** HotSpot 8 `-XX:ObjectAlignmentInBytes` 8 12 ### Java reference ******** #### Java reference  #### reference  reference reference HotSpot ## - Java JVM - Java - Chapter 2. The Structure of the Java Virtual Machine - JVM - - Java new String - - - JVM - RednaxelaFX - - - -
Web Proxy Viewer |
New URL
|
Original Page