Learning THE JVM virtual machine is a rather boring process. At the beginning, I basically did not understand it, and then I was vaguely aware of it. Finally, I felt as if I knew something, and then I felt that I still did not understand it.

I think after learning relevant theoretical knowledge, I can better understand what I have learned and check whether I really understand it by asking questions and answering in addition to carrying out practical operations.

Today we are going to look at the runtime data area of the Java Virtual machine, Include Program Counter Register, Java Virtual Machine Stack, Native Method Stack, Heap, Method area Area), and the run-time Constant Pool.

It is mainly based on The Java Virtual Machine Specification (Java SE 8 Edition).

So let’s move on to questions and answers.

Public problems

Which areas are shared by threads, and when are they created and destroyed?

Areas of memory shared by threads, including the Java heap and method areas.

It is created when the vm starts and destroyed when the VM exits.

Which areas are thread private and when are they created and destroyed?

A thread-private area of memory, including program counters, Java virtual machine stacks, and local method stacks.

It is created when the thread is created and destroyed when the thread terminates.

Should the memory used by the Java virtual machine stack, heap, and method area be contiguous?

None. The memory for… Does not need to be contiguous.)

All fields throw OutOfMemoryError?

It isn’t. Program counters are the only area where the Java Virtual Machine specification does not specify any OutOfMemoryError cases.

Program counter

What is a program counter? What’s the use?

Program Counter, also called PC Register (Program Counter Register).

Can be thought of as a line number indicator of the bytecode being executed by the current thread.

In the conceptual model of virtual machine, bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed, and basic functions such as branch, loop, jump, exception handling, thread recovery and so on need to be completed by this counter. (Specific virtual machine implementations can take a more efficient approach)

Are program counters different for Java and native methods?

There’s a difference.

If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed.

This counter value is undefined if the thread is executing a Native method.

Why should each thread have a program counter?

So that the thread can be switched back to the correct execution position. A context switch between threads requires that the program counter be saved first, and that execution continues from where the program counter was last saved when the thread resumes.

When multiple threads are executing simultaneously, multiple program counters are required to indicate the next instruction to be executed.

Java virtual machine stack and native method stack

What’s the difference?

They do basically the same thing.

The difference is that the virtual machine stack serves the Java methods executed by the virtual machine, while the Native method stack serves the Native methods used by the virtual machine.

What does the Java virtual machine stack do?

Used to store stack frames.

The Java virtual machine stack works much like a stack in a traditional language (such as C), storing local variables and results that have not yet been computed. In addition, it plays an important role in method calls and returns.

When does the Java virtual machine stack throw what exceptions?

StackOverflowError is thrown if the stack size allocated by a thread request exceeds the maximum size allowed by the Java virtual machine stack.

An OutOfMemoryError is raised if the Java virtual stack can be dynamically extended and sufficient memory cannot be allocated during the extension, or when a new thread is created that does not have enough memory to create the corresponding virtual stack.

What is the default size of the Java virtual machine stack? How to modify?

The default vm stack size is 1 MB (previously 256 KB), which can be set using the -xss parameter.

What is a stack frame and when is it created and destroyed?

Stack frames are data structures used to store data and partial process results (operand stack, local variable table). They are also used to handle dynamic linking, method return values, and dispatch exceptions.

Stack frames are created with method invocation and destroyed with method completion. Method ends with an exception thrown that is not caught in the method.

What’s inside the stack frame?

This is mainly a local variable table, operand stack, and a reference to a pool of run-time constants to the class to which the current method belongs.

Can stack frames of different threads refer to each other?

Can’t. The Java virtual machine stack is thread-private, and thus stack frames are thread-local private data.

How to understand the current stack frame? How do method calls affect stack frames?

The stack frame is created as the method is called, the currently executing method is called the current method, and the corresponding stack frame is the current stack frame.

  • When the current method calls a new method, a new stack frame is created. As control of the program is transferred to the new method, the new stack frame becomes the new current stack frame.
  • When the method returns, the current stack frame returns the result of the method’s execution to the previous stack frame. The virtual machine then discards the current stack frame, making the previous stack frame become the current stack frame again.

Local variable scale

What is a local variable scale

Local Variable Table is a set of Variable value storage space used to store method parameters and Local variables defined within a method.

The Java virtual machine uses a local variable table for parameter passing during method calls.

Where is the local variable scale stored?

Each stack frame has its own local variable table, and the length of the local variable table in the stack frame is determined by compile time.

The local variable table is stored in the binary representation of a class or interface (such as a class file), saved through the code attribute of the method and provided to the stack frame.

Is the local method stack required?

If the Java VIRTUAL machine does not support native methods or does not rely on a traditional stack itself, then it is possible not to provide a native method stack.

If a local method stack is supported, this stack is typically allocated by thread at thread creation time.

When and what exceptions are thrown by the local method stack?

Same as above: When and what exceptions are thrown by the Java virtual machine stack?

The Java heap

The role of the heap

This is where almost all object instances are allocated memory.

Do all object instances really need to be allocated on the heap?

Typically, all object instances, including arrays, are allocated on the heap.

However, as JIT compilers and escape analysis techniques mature, on-stack allocation and scalar replacement optimization techniques will lead to some subtle changes, and it will become less and less absolute that all objects are allocated on the heap.

How will the heap be divided?

Today’s garbage collectors generally use generational collection algorithms, so the Java heap is typically divided into new generation and old generation. The Cenozoic generation can be further divided into Eden region, From Survivor region and To Survivor region.

Why is heap memory partitioned?

In order to better manage memory, better and faster reclamation and allocation of memory.

Since the life cycle of most objects is usually relatively short, they can be divided into Cenozoic and old age according to their life cycle. This design is conducive to adopting more suitable garbage collection algorithms and improving recycling efficiency.

Most objects of the new generation have a short life cycle. Using the copy algorithm, only a few objects need to be moved, and the rest are directly cleaned up.

In the old age, the object life cycle is relatively long, so it is more efficient to use the tag cleaning or tag cleaning algorithm.

Local thread allocation buffer TLAB can allocate memory more safely and faster.

What is TLAB?

TLAB refers to the Thread Local Allocatoin Buffer, which is a small chunk of memory allocated by each Thread in the Java heap before allocating memory to an object.

What does TLAB do?

On the one hand, it can solve the thread safety problem of multi-thread concurrent memory allocation, on the other hand, it can allocate memory faster.

How does TLAB work?

Before allocating memory to objects, each thread preallocates a small chunk of memory (the local thread allocation buffer) in the Java heap. Whichever thread wants to allocate memory is allocated on the TLAB of that thread. When the TLAB runs out, you need to apply for a new block of memory.

Methods area

What is a method area and what does it do?

The method area is a logical part of the heap.

The function of method sections is very similar to that of storage area for Compiled code in traditional languages or text segments of operating system processes.

It stores structural information for each class, such as runtime constant pools, field and method data, and bytecode content for constructors and common methods, as well as special methods (instance initializers, class or interface initializers) used for class, instance, and interface initialization.

(Stores class information that has been loaded by the virtual machine, constants, static variables, code compiled by the just-in-time compiler, and so on.)

OutOfMemoryError is thrown when the method area cannot meet memory allocation requirements.

What is the purpose of garbage collection in the method area?

This is mainly for constant pool recycling and offloading of types. Simple virtual machine implementations can choose not to implement garbage collection and compression in this area.

Student: Is the method region the permanent generation?

They’re essentially not equivalent.

The method area is an integral part of the Java virtual machine run-time data area, and the persistent generation is an implementation of a specific virtual machine.

It is simply because the designers of the HotSpot VIRTUAL machine chose to extend GC generation collection to the method area, or to implement the method area using persistent generation.

For other virtual machines, there may be no concept of permanent generation.

Are there disadvantages to using persistent generation to implement method areas?

Using persistent generation to implement method areas is not a good idea at this point because it is more likely to run into memory overflow problems.

In HotSpot, currently released in JDK 1.7, the string constant pool that was placed in the persistent generation has been removed.

JDK 1.8 uses metadata Spaces instead of persistent generations, primarily to store metadata for classes.

Run-time constant pool

What is the runtime constant pool?

Is the runtime representation of the constant pool table for each class or interface in the class file.

It includes several different constants, from numeric literals known at compile time to method or field references that must be parsed at run time.

A run-time constant pool is similar to a Symbol table in a traditional language, except that it stores a broader range of data.

When a class or interface is created, OutOfMemoryError is thrown when the method area cannot meet memory allocation requirements.

Using the Javap command, you can view the constant pool information of a class file.

When is it created?

At virtual machine startup time, after classes and interfaces are loaded into the virtual machine, the corresponding runtime constant pool is created.

Where is it distributed?

Each run-time constant pool is allocated in the Java virtual machine’s method area.


Idle to boring, also opened a free knowledge planet. Talk about technology, but also talk about life.


Illustration: www.pexels.com

reference

The Java Virtual Machine Specification, Java SE 8 Edition

Java Virtual Machine Specification (Java SE 8 edition)

Deep Understanding of the Java Virtual Machine. 2nd edition by Zhiming Zhou

Java Class file structure example analysis

Java Class File Structure Analysis (part 1)

Example analysis of Java Class File Structure

JVM instruction analysis example

JVM Instruction Analysis Example 1 (Constants, local variables, for loops)

JVM Instruction Analysis Example 2 (arithmetic operations, constant pools, control structures)

JVM Instruction Analysis Example 3 (method calls, class instances)

JVM instruction analysis example 4 (array, switch)

JVM Instruction Analysis Example 5 (operand stack)

Personal public account

For more articles, please pay attention to the public number: binary road