当前位置:网站首页>[JVM Series 2] runtime data area
[JVM Series 2] runtime data area
2022-06-13 03:08:00 【Louzai】
Mainly about Java The time zone of the runtime , Include Java Pile up 、 Virtual machine stack 、 Native Method Stack 、 Method area and program counter .
Previous selections ( Welcome to forward ~~)
How to treat programmers 35 Career crisis at the age of 20 ?
Java A complete set of learning materials (14W word ), It took half a year to sort out
I've had liver for three months , Wrote for you GO Core manual
Message queue : From selection to principle , One article will take you all to master
Liver for a month ETCD, from Raft From principle to practice
Preface
Last article 《【JVM series 1】JVM Memory structure 》 It has been told that JVM Memory structure , In fact, this explanation is not complete , Only explained Java Part of the heap , Now I will add other parts .
Run time data area
What is a runtime data area ?
Java The program is running , Would be JVM Separate a memory area , This memory area can be divided into a runtime data area again , The runtime data area can be roughly divided into five parts :
Java Pile up (Heap)
Many students who do development , Will pay special attention to the heap and stack , Does this explain the importance of heap and stack from another point of view ?
In a word : Stack operation , Stack storage . The virtual machine stack is responsible for running the code , The virtual machine heap is responsible for storing data .
Put the dry goods up first , First ,Java The heap area has the following characteristics :
What's stored is us new The people who came here , Do not store basic types and object references .
Because a lot of objects are created , The garbage collector mainly works in this area .
Thread sharing area , So it's not thread safe .
Can happen OutOfMemoryError.
Actually ,Java The pile area can also be divided into Cenozoic and old generation , The new generation can be further divided into Eden District 、Survivor 1 District 、Survivor 2 District . For specific proportional parameters , Take a look at this picture .
For details, please refer to 《【JVM series 1】JVM Memory structure 》
Virtual machine stack (JVM Stacks)
Java The virtual machine stack is also a focus of developers , Again , Put the dry goods up first :
Java The virtual machine stack is thread private , Each thread has its own virtual machine stack , It has the same life cycle as a thread .
The virtual stack describes Java Memory model for method execution : Each method is executed with a stack frame created at the same time (Stack Frame) Used to store local variables 、 Stack operation 、 Dynamic links 、 Method exit information . Each method is called until the procedure completes , This corresponds to the process of a stack frame in the virtual machine stack from push to push .
Store basic data types (boolean、byte、char、short、int、float、long、double) And references to objects (reference type , It's not the same as the object itself , According to different virtual machine implementation , It might be a reference pointer to the starting address of the object , It may also point to a handle representing an object or other location related to the object ) and returnAddress type ( Points to the address of a bytecode instruction ).
There may be two kinds of anomalies in this area : If the stack depth of the thread request is greater than the depth allowed by the virtual machine , Will throw out StackOverflowError abnormal ; If the virtual machine stack can be expanded dynamically ( Most of the current Java Virtual machines can be dynamically expanded , It's just Java Fixed length virtual machine stack is also allowed in the virtual machine specification ), Thrown when the extension cannot request enough memory OutOfMemoryError abnormal .
Native Method Stack (Native Method Stacks)
The role of the local method stack is very similar to that of the virtual machine stack , The difference is that the virtual machine stack executes for the virtual machine Java Method ( That's bytecode ) service , The local method stack is used by the virtual machine Native Method service .
The language used in the VM specification for methods in the native method stack 、 Usage and data structure are not mandatory , So the virtual machine can realize it freely . Even some virtual machines ( for example Sun HotSpot virtual machine ) Directly combine the local method stack and virtual machine stack into one . Same as the virtual machine stack , The local method stack area is also thrown StackOverflowError and OutOfMemoryError abnormal .
What is? Native Method? Simply speak , One Native Method It's just one. java Call not java Code interface . One Native Method It's such a java Methods : The implementation of this method consists of java Language implementation , such as C. This feature is not java Unique to , Many other programming languages have this mechanism , For example C++ in , You can use it. extern "C" inform C++ The compiler calls a C Function of .
Method area (Method Area)
The method area is also a focus area , The main features are as follows :
Thread sharing area , So this is a thread unsafe area .
It is used to store class information that has been loaded by the virtual machine 、 Constant 、 Static variables 、 Real time compiler compiled code and other data .
When the method area cannot meet the memory allocation requirements , Will throw out OutOfMemoryError abnormal .
although Java The virtual machine specification describes the method area as a logical part of the heap , But it has an alias called Non-Heap( Non heap ), The purpose should be with Java Heap differentiation . For habits in HotSpot For developers who develop and deploy programs on virtual machines , Many people like to call the method area “ Forever ”(Permanent Generation), They're not essentially equivalent , So what's the difference between them ? The method area is Java Definition in virtual machine specification , It's a norm , And permanent generation is a kind of realization , One is the standard and the other is the realization . however Java 8 There will be no permanent generation in the future , Meta space replaces permanent generation .
Java This is a very loose area for virtual machines , Except for and Java The heap does not need continuous memory and can be fixed size or expandable , You can also choose not to implement garbage collection . Relatively speaking , Garbage collection is relatively rare in this area , But it's not the data that goes into the method area like the name of the permanent generation “ permanent ” There is . The target of this area is to recycle the constant pool and unload the type , In general this area of recycling “ achievement ” More unsatisfactory , In particular, type uninstall , The conditions are pretty tough , But the recycling of this part of the area is really necessary .
Program counter (Program Counter Register)
The program counter is very simple , Surely no one is Java For beginners , We should also understand the concept of thread and process ?( Soul torture , Do you understand ?) It doesn't matter if I don't understand , Let me make it clear to you .
A process is the smallest unit of resource allocation , Thread is CPU Minimum unit of scheduling , A process can contain multiple threads , Java The thread obtains by preemption CPU The enforcement of . Now you can think about the following scenario .
One time , Threads A get CPU The enforcement of , Start execution of internal procedures . But threads A Your program is not finished yet , At some point CPU The execution right of is by another thread B Snatched . Later, through the thread A Our unremitting efforts , Again CPU The enforcement of , So thread A The program has to be executed from the beginning ?
At this time, the program counter appears , Its function is to record the position executed by the current thread . such , When the thread regains CPU When it comes to executive power , Start directly from the location of the record , Branch 、 loop 、 Jump 、 Exception handling also depends on this program counter to complete . Besides , The program counter also has the following features :
Thread private , Each thread has a program counter , So it's thread safe .
The only piece doesn't exist OutOfMemoryError Region , Maybe the designer doesn't think it's necessary .
Code example analysis
about Java Pile up 、 Method area 、 Thread exclusive area ( Mainly virtual machine stack ), Methods are executed with threads , The original type of local variables and references are stored in the program stack , And reference related objects such as String, It's all in the pile .
In order to better understand the above picture , Let's take an example :
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
public class HelloWorld {
private static Logger LOGGER = Logger.getLogger(HelloWorld.class.getName());
public void sayHello(String message) {
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.YYYY");
String today = formatter.format(new Date());
LOGGER.info(today + ":" + message);
}
}
Let's review what we learned earlier :
The heap stores us new The people who came here , Do not store basic types and object references ;
Stack storage stores basic data types 、 References to objects and returnAddress type ;
The method area stores class information that has been loaded by the virtual machine 、 Constant 、 Static variables 、 Real time compiler compiled code and other data .
The data of this program is stored in memory as follows :
It can be seen that the data is stored as follows :
Java Pile up : object HelloWorld、 object SimpleDateFormat、 object String And the object LOGGER;
Thread exclusive area ( Mainly virtual machine stack ):message References to 、formatter References to 、today References to ;
Method area : Class information SimpleDateFormat、 Class information Logger、 Class information HelloWorld、 Method sayHello(), It also includes all methods of class information .
This static Logger LOGGER, Because it's a static variable , Should it belong to the method area ?
Welcome to like it more , More articles , Please follow the WeChat public account “ Louzi's way to advancement ”, Focus , Neverlost ~~
边栏推荐
- MySQL 8.0 installation free configuration method
- Mongodb distributed cluster deployment process
- JS merge multiple string arrays to maintain the original order and remove duplicates
- On the limit problem of compound function
- Open source - campus forum and resource sharing applet
- Collection of IOS development interview and underlying learning videos
- Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
- Level II C preparation -- basic concepts of program design
- Ijkplayer source code - choose soft decoding or hard decoding
- 最近最少使用缓存(来源力扣)
猜你喜欢
Introduction to facial expression recognition system - Technical Paper Edition
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
开源-校园论坛和资源共享小程序
Flutter reports an error type 'Int' is not a subtype of type 'string' wonderful experience
Use of jstack
JVM GC (V)
Summary of the latest IOS interview questions in June 2020 (answers)
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
随机推荐
Applet image component long press to identify supported codes
Beginner development process_ Project development FAQs
How to select fund products? What kind of fund is a good fund?
The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
Pytorch record: pytorch variables parameter and buffer. self. register_ buffer()、self. register_ parameter()
Linked lists: rearranging linked lists
Prometheus node_ Exporter installs and registers as a service
Professional database management software: Valentina Studio Pro for Mac
JVM class loader (2)
Use of jstack
C 10 new features_ C 10 new features
Wechat applet switch style rewriting
关于复合函数的极限问题
Review notes of RS data communication foundation STP
Wechat applet coordinate location interface usage (II) map interface
mysql索引
Personal understanding of grammar sugar
Keil removes annoying st link update tips
JVM virtual machine stack (III)
Scala implements workcount