A few days ago, I was asked a static… The interviewer is just asking about basic usage. It should be about the JVM.
Static base usage
- Just a quick introduction
Modifying variables are called static member variables
Decorated methods are called static member methods
Modify inner classes. Normal classes are not allowed to be declared static, only inner classes are
Modify static code blocks (here’s a random example on the web)public class Student { private static String SchoolName; private static int nums; Static {student.schoolname = "SchoolName "; Student.nums = 0; }}Copy the code
Let’s look at it from a memory perspective
- In simple terms, static variable data is stored in the static section of the method section (shared data section), so it is also called shared data for objects
Image random run-time data area (also called memory structure) from here on down the concept is nonsense…
Er… By the way, there are several concepts in this diagram, and specific features in the JVM can be pulled.
1.Method Area
The bytecode for all fields and methods of the class, as well as special methods such as constructors, and the interface code are also defined here. In simple terms, all the methods defined information is stored in the area, static variables + constant + class information (constructor/interface definition) + runtime constants are pool area, while the Java virtual machine specification, described the method of area as a logical part of the Heap, but it has an alias called Non – Heap Heap (not), The purpose should be to distinguish the hotspot virtual machine from the Java heap.
2. The Heap Heap
Almost all objects are allocated memory on the heap. An OutOfMemoryError is raised if an object cannot be allocated memory in this space. It is also the main area managed by the garbage collector.
2.1 Young Generation
An area where classes are born, grow, and die, where a class is created, applied, and finally collected by the garbage collector to end its life. The Cenozoic is divided into two parts: Eden Space and Survivor Space, where all classes are newly created. The surviving zones are divided into From and To zones. When Eden space runs out, the program needs to create objects again. The GARBAGE collector of the JVM makes a Minor GC for Eden and destroys objects in Eden that are no longer used by other objects. Then move the remaining objects in the Eden zone to the From Survivor zone. If the From Survivor zone is also full, then the zone is garbage collected and moved To the To Survivor zone.
2.2 Old Generation
Cenozoic objects are still in stock after multiple GC moves to old age areas. If the old age is also Full, a Major GC (also called a Full GC) will occur to clean up the old age area. An OOM (OutOfMemoryError) exception will be raised if the old section fails to save objects after Full GC
2.3 Meta Space
After JDK1.8, the permanent generation has been replaced by a meta-space, which is an implementation of the method area in the JVM specification. The difference is that the metadata area is not in the virtual machine, but is used in local memory. The permanent generation is in the virtual machine, and is logically part of the heap, but physically not. Why the permanent generation was removed and here’s an overview of why
3. The Stack Stack
Memory model of Java thread execution method, a thread corresponding to a stack, each method at the same time of execution will create a stack frame (local variables used to store, the operand stack, the dynamic linking, export information such as the method) there is no garbage collection problem, as long as the thread one end of the stack is released, the life cycle and thread
A: Native Method Stack
It is similar to a stack, except that the Java stack serves the JVM to execute Java methods, while the native method stack serves the JVM to execute native methods. Register native methods and load the local method library at Execution Engine time
5.Program Counter Register
It is a pointer to the method bytecode in the method area (used to store the address that points to the scare instruction and the code for the instruction to be executed), read by the execution engine for the next instruction, and is a very small memory space, almost negligible