当前位置:网站首页>JVM quick start
JVM quick start
2022-07-06 08:35:00 【So, dream】
List of articles
JVM brief introduction
What is? JVM?
JVM yes Java virtual machine (Java Virtual Machine) For short . It's called a virtual machine , Because it is an abstract computer , There is a complete hardware architecture inside ( register , Stack , Instruction system, etc ).
Simply speaking ,Java A virtual machine is a perform Java Bytecode virtual computer .
What is a virtual machine ?
Definition : A virtual machine is a virtual computer . It's a software , Can simulate a series of virtual computer instructions .
Virtual machines can be divided into System virtual machine and Program virtual machine Two kinds of .
JVM The role of
Java Virtual machine is The running environment of binary bytecode , Responsible for loading bytecode into its interior , explain / Compile to the corresponding platform (Linux,Mac,Windows etc. ) Execution of machine instructions on .
JVM Characteristics
- Platform independence
- A compilation , Run anywhere
- Automatic memory management
- Automatic garbage collection
JVM The location of
It was said that , Virtual machine is a software that simulates the instructions of virtual computer . So it It's essentially a piece of software , Running on the operating system .
JVM Structure
JVM The structure of can be simply represented by the figure below :
Java Memory optimization mainly focuses on heap , The method area is a special heap area .
Class loader
Class loader (ClassLoader) be used for Load the bytecode file into JVM Middle to run .
JVM Three layers are provided in ClassLoader:
- Bootstrap classLoader: Boot class loader , Mainly used for loading Java Core class library of .
- ExtClassLoader: Extend the classloader , Used for loading jre/lib/ext Some of the extended jar package .
- AppclassLoader: system class loader , Used to load classes in the application .
Parent delegate mechanism
The parental appointment mechanism is JVM load .class The mechanism of . When loading a class , The system class loader delegates upward to the extension class loader , The extension class loader delegates upward to the boot class loader . If the boot class loader contains this class , Load successfully . Otherwise, it is handed down to the extension class loader to load , The extension class loader cannot load , It will be loaded by the system class loader .
Advantages of parental delegation :(1) Avoid class reloading , Once a class is loaded by the parent loader , It will not be loaded by the subclass loader (2) Protect program security , Prevent the core file from being tampered
Sandbox security mechanism
Sandbox is a security mechanism in the field of computer security , Provide an isolated environment for running programs . If JVM It's a big box , If I run an unknown program directly inside , Is likely to JVM Other internal procedures have adverse effects . therefore , We can do it in JVM Zhongjian has a small box ( The sandbox ), Let it run in this small box .
If the code in the sandbox needs to access the operating system and local resources , How to ensure safety ? Sandbox security mechanism provides different for different sandboxes jurisdiction , Call the operating system and local resources according to permissions . for example , Core classes have the highest permissions , Remote code has lower permissions .
native keyword
native
It means local , When the method is native
Keyword modification , Express Java The scope of action of has not reached , Will call libraries in other languages . Execute to be native
The modified method will enter the local method stack , call JNI (Java Native Interface) To operate other programming languages .
Native Method Stack : For registration native
Method , Load the local method library when the execution engine executes
JNI: Local method interface , Call the class libraries of other languages through this interface
Java Use native
Keywords are for extension Java Use , Can be in Java Using other programming languages , So that it can stand .
Program counter
Each thread has a program counter , Threads are private . It's a pointer , Point to the bytecode in the method area , Tell the execution engine which instruction to execute now .
Method area
Method area Shared by all threads , All field and method bytecodes , And some special methods , Such as constructor , The interface code is also defined here . Simply speaking , Information about all defined methods is stored in this area , This area is a shared area .
Static variables 、 Constant 、 Class object ( Construction method 、 Interface definition ) The runtime constant pool exists in the method area , But variable instances exist in heap memory
Stack
A stack is a last in, first out data structure . The operation of stack supervisor , Life cycle is synchronized with thread .
Thread end , Stack memory is also released , So for the stack , There is no recycling .
The contents stored in the stack are :
- Eight basic data types
- References to objects in the heap
Pile up
Instances in the heap used to store objects . The stack is thread level , There is only one heap in the virtual machine .
The memory of the heap can be further subdivided into : New Area 、 Elderly area and meta space . The new area can be subdivided into Eden Park 、 survive 0 Area and survival 1 District .
New Area
Eden Park is the place where objects are generated , When Eden Park is full , Will trigger a lightweight GC, Objects that have not been cleared will be sent to the survival area . When the new area is full , Will trigger a reset GC, heavy GC The surviving objects will be sent to the elderly area . Besides , Without setting , The new area has experienced 15 Second light GC The target will be sent to the elderly .
Old age area
The old area is used to store the heavy experience in the new area GC Or reach light GC Objects that still survive after the threshold number .
Meta space
Meta space is resident in memory , What's stored is Java Some environment or class information at runtime .
IDEA Set in JVM Size
At the project construction site , double-click Edit Configurations
In the pop-up page , Set the size of the virtual machine
-Xms8m -Xmx8m -XX:+PrintGCDetails
in -Xms、-Xmx
Used to set the initial memory allocation size and the maximum allocated memory ,-XX:+PrintGCDetails
For printing GC Details of .
Garbage collection mechanism
Garbage recycling is mainly concentrated in Eden Park and the elderly area .
Types of garbage collection include light GC and heavy GC, light GC Only clean the new areas , and heavy GC It's the whole picture GC.
GC Common algorithms of include Replication Algorithm 、 Mark removal method and mark sorting method .
Copy algorithm
Here we will be survivors 0 District And survivors 1 The district is called from Area and to District . When Eden Park is full , Will trigger a light GC, The surviving object enters to District , And then from Copy the existing objects in the area to to In the area ; The final will be from Empty the area and Exchange from Area and to The location of the area .
The advantage of this method is There will be no memory fragmentation , And the time complexity is low , The disadvantage is to maintain an empty to District , It's a waste of resources .
Mark removal method
Mark removal method includes two steps , First scan , Mark the elements to be cleared , Then scan again to clear the object .
The advantages of this method will not cause a waste of resources , But it has high time complexity , And will produce memory fragments .
Mark up
Mark sorting method is based on mark clearing method , By moving objects, memory fragmentation is avoided .
Compared with mark removal , The time complexity of this algorithm is higher , But there will be no memory fragmentation .
边栏推荐
- LDAP Application Section (4) Jenkins Access
- vulnhub hackme: 1
- Visual implementation and inspection of visdom
- Colorlog结合logging打印有颜色的日志
- egg. JS getting started navigation: installation, use and learning
- Leetcode skimming (5.29) hash table
- tree树的精准查询
- 2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
- 指针进阶---指针数组,数组指针
- Trying to use is on a network resource that is unavailable
猜你喜欢
2022.02.13 - NC003. Design LRU cache structure
Sort according to a number in a string in a column of CSV file
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
Computer cleaning, deleted system files
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
Ruffian Heng embedded bimonthly, issue 49
vulnhub hackme: 1
软件卸载时遇到trying to use is on a network resource that is unavailable
【MySQL】锁
随机推荐
CISP-PTE实操练习讲解
Computer cleaning, deleted system files
leetcode刷题 (5.29) 哈希表
IOT -- interpreting the four tier architecture of the Internet of things
leetcode刷题 (5.31) 字符串
leetcode刷题 (5.28) 哈希表
Deep analysis of C language pointer
egg. JS project deployment online server
2022.02.13 - NC002. sort
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
Chrome浏览器的crash问题
Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
China polyether amine Market Forecast and investment strategy report (2022 Edition)
swagger设置字段required必填
软件卸载时遇到trying to use is on a network resource that is unavailable
Function coritization
Image, CV2 read the conversion and size resize change of numpy array of pictures
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
游戏解包的危害及资源加密的重要性