当前位置:网站首页>JVM architecture learning notes

JVM architecture learning notes

2022-06-09 08:15:00 More books

Catalog

JVM Where's your location ?

JVM Complete system diagram

One 、 Class loader ClassLoader

Expand

1. Three classLoader:

2.classLoader Parent delegate mechanism :

3. Sandbox security mechanism :

4. Program compilation and loading sequence :

Two 、 Native Method Stack <- Local method interface <- Local method library

3、 ... and 、 Program counter (pc register )

Four 、 Method area

5、 ... and 、java Stack ( Stack stack)

6、 ... and 、 Pile up

Detailed explanation :

1. The new generation (PSYoungGen): 

2. Old age ( Retirement area ParOldGen):

3. Meta space Metaspace(Java7 It is called permanent generation or permanent generation ):


JVM Where's your location ?

Conclusion :JVM Is running on the operating system , It has no direct interaction with the hardware

JVM Complete system diagram

One 、 Class loader ClassLoader

Responsible for loading class file , take class File bytecode content loaded into memory , And convert this content into the runtime data structure in the method area , also ClassLoader Only responsible for class Loading of files , As for whether he can run , By Execution engine Decide to load class After that is the template Class, This template stores the structure information of the class , A template can instantiate multiple instances

Expand

1. Three classLoader:

Start the loader Bootstrap(C++)---> Extended loader Extension(Java)---> Application class loader AppClassLoader, They are grandfather and grandson , They are all abstract classes , Need to be inherited . There are also user-defined loaders Java.lang.ClassLoader Subclasses of , The user can customize the loading method of the class , By inheritance ClassLoader class

2.classLoader Parent delegate mechanism :

When you receive a java File class load request , That is, all requests will be sent to the startup load class , First from the root loader (Bootstrap) Start looking down , I can't find a throw ClassNotFound abnormal , Ensure the safety of the sandbox

3. Sandbox security mechanism :

It is implemented by the parent delegation mechanism , Only load from Bootstrap The first file found down , Guarantee Java The source code of is not polluted

4. Program compilation and loading sequence :

Static code block ( Load only once )> Tectonic block > Construction method

Two 、 Native Method Stack <- Local method interface <- Local method library

Local interface The function of is to integrate different programming languages into java used , His original intention was to merge C/C++ Program , So a special area is opened up in the memory to process , Marked as native Code , His specific practice is Native Method Stack Registered in native Method , Load... While executing engine execution native libraies.

therefore Native Method Stack It's used to store The loaded data of the local method library

3、 ... and 、 Program counter (pc register )

  • Also called program counter , It's a pointer , The address where the next instruction is stored , Point to the next command to execute , It records the call and execution between methods
  • Each thread has a program counter , Like the stack, it is thread private , Memory is small and almost nonexistent GC, No memory overflow
  • If the execution is Native Method , The counter is empty

Four 、 Method area

  • All threads share , There is GC, It's just a small amount , Most of the GC In the pile
  • The area of runtime memory shared by threads , Used to store the structure information of each class , For example, runtime constant pools 、 Field and method data 、 Bytecode content of construction method and common method 、 Templates Class、static Variable . The method area is the specification , Different virtual machines implement different , For example, permanent generation PermGen space and Meta space Metaspace
  •  java7 Before : Method area f = new Forever    java8 after : Method area f = new Meta space
  • Class's instance methods and instance objects are stored in heap memory , It has nothing to do with the method area

5、 ... and 、java Stack ( Stack stack)

  • Each method is executed with a stack frame created , Used to store local variables , The stack of operands , Dynamic links , Method exit information , The process of each method from call to execution , Corresponding to a stack frame in the virtual machine stack to stack out of the process . Stack size and details JVM The realization of , Usually in 256-756 Between .
  • Thread private , non-existent GC problem ,8 Basic type variables in + Object reference variable + Instance methods are allocated in the stack memory of functions
  • Java Methods are pushed onto the stack , be called Stack frame , main Methods are first pressed on the stack , Stack frames are memory areas , Is a dataset of methods and runtime data
  • Stack frame is mainly saved 3 Class data :
    1. Stack frame data : Include class files 、 Such method
    2. Stack operation : Record the stack 、 Stack operation
    3. The local variable : Input and output parameters and variables in methods

6、 ... and 、 Pile up

        Java Pile up (Java Heap) Is the largest piece of memory managed by a virtual machine .Java Heaps are made up of A memory area shared by threads , Created when the virtual machine starts . The sole purpose of this memory area is to hold object instances ,Java In the world “ almost ” All object instances allocate memory here .
        Java The heap is the memory area managed by the garbage collector , So it's also called “GC Pile up Garbage Collected Heap, Fortunately, there is no translation in China rubbish dump ). From the perspective of reclaiming memory , Because most of the modern garbage collectors are based on the theory of generational collection , therefore Java There are always The new generation ”“ Old age ”“ Forever ”“Eden Space ”“From Survivor empty between ”“To Survivor Space Wait for the noun

Detailed explanation :

1. The new generation (PSYoungGen): 

        1. Eden (Eden Space): new Object exists in this area ,GC( Also called  YGC  Or light GC  or  MinorGC) In this area ; It turns on when the memory exceeds the threshold GC Clear data , Objects and static variables are not moved to S0 District .                
        2. Survivor 0 District (Survivor 0 Space,  Also called S0  or  from District ): Every time GC It also performs the process of copying, emptying and exchanging
        3. Survivor 1 District (Survivor 1 Space, Also called S1  or  to District ): And S0 The zone mechanism is the same every time GC There will be 0 Area and 1 The process of copying, emptying, and exchanging areas , Whoever is empty is 1 District . 
        The internal operation of the new generation :
                GC take Eden The surviving objects are copied to S0 Empty after the area Eden District , Again GC Scan after Eden and S0 Area for GC, Reclaim the surviving objects and copy them to to District ( If the age of the object reaches the standard of the elderly area ( The default is 15 Time ) Then copy to the elderly area ), At the same time, the age of these objects +1, You need to scan every time Eden Area and S0 and S1 District , Conduct MinorGC The process

2. Old age ( Retirement area ParOldGen):

         When the object exceeds the memory threshold , Turn on Full GC( Also called FGC Or heavy GC Or the whole situation GCMajorGC), many times FGC You can't make room after , Will throw out OOM(OutOfMemoryError) Heap memory overflow error ,Major GC Than Minor GC About ten times slower , Every time Major GC often ( Not absolute ) Accompany once Minor GC

3. Meta space Metaspace(Java7 It is called permanent generation or permanent generation ):

         To hold JDK It's self-contained Class,Interface Metadata ( structural information ), That is, the class information necessary for the running environment . Data loaded into this area will not be GC, close JVM The memory occupied by this area will be released
         Corresponding method area , Although logically JVM The specification describes the method area as a logical part of the heap , But it is not physically included in the heap . A meta space is an implementation of the method area , That is, the method area is the interface , Meta space is the realization of .
         The biggest difference between permanent generation and meta space : For permanent use JVM Heap memory for , and Java8 The following meta space exists in the local physical memory , The string constant pool is also in the meta space

Heap memory tuning

  • The meta space exists in the local physical memory , Only limited by local memory , The string constant pool is also in the meta space , Less OOM, The default maximum JVM Heap Heap memory -Xmx Physical memory only 1/4, Minimum heap memory -Xms For physical memory 1/64
  • Runtime.getRuntime().totalMemory; // Get the maximum amount of memory , Runtime.getRuntime() by Runtime Data Area object
  • JVM Adjustable parameter :-Xms It should be equal to -Xmx, avoid GC Competing with applications for memory results in JVM The problem of theoretical value varying from high to low
原网站

版权声明
本文为[More books]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090811047027.html