当前位置:网站首页>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 .
边栏推荐
- 延迟初始化和密封类
- Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
- Sublime text using ctrl+b to run another program without closing other runs
- On the inverse order problem of 01 knapsack problem in one-dimensional state
- Restful API design specification
- leetcode刷题 (5.29) 哈希表
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
- 查看局域网中电脑设备
- Indentation of tabs and spaces when writing programs for sublime text
- sublime text中conda环境中plt.show无法弹出显示图片的问题
猜你喜欢
egg. JS project deployment online server
Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
The harm of game unpacking and the importance of resource encryption
Mobile phones and computers on the same LAN access each other, IIS settings
win10系统中的截图,win+prtSc保存位置
Deep analysis of C language pointer
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
堆排序详解
Computer cleaning, deleted system files
View computer devices in LAN
随机推荐
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
egg. JS getting started navigation: installation, use and learning
生成器参数传入参数
VMware virtualization cluster
leetcode刷题 (5.31) 字符串
leetcode刷题 (5.28) 哈希表
Bottom up - physical layer
Configuring OSPF load sharing for Huawei devices
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
Verrouillage [MySQL]
【MySQL】锁
The network model established by torch is displayed by torch viz
visdom可视化实现与检查介绍
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
Leetcode question brushing (5.28) hash table
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
电脑清理,删除的系统文件
China polyether amine Market Forecast and investment strategy report (2022 Edition)
Image,cv2读取图片的numpy数组的转换和尺寸resize变化