One of the most important features of the Java language: cross-platform use is due to the existence of the JVM.

If you want to steadily advance Java development, there is no way around learning the JVM.

1. Why learn the JVM?

As we all know, to do Java development, you must install the JDK on your computer. After installing the JDK, there will be a directory for the JRE, which provides the software environment —-JVM.

During the development process, when we have a memory leak, we should not think about -xms setting or stack -xSS or restart to solve the memory leak, but should think about, why does it happen? What causes it? How do you optimize?

2. What is JVM?

A Java Virtual Machine (JVM) is a Virtual computer that runs on top of an operating system.

We focused on the virtual machine that comes with the HotSpot VM JDK, which has two great advantages: accurate GC + HotSpot code detection. Precise GC lets the JVM know what the type of data is at a location in memory.

Such as whether the data in the current memory location is an integer variable or a reference type. This allows the JVM to quickly determine the location of all reference types, making GC roots enumeration more targeted.

The behavior of the JVM when a piece of code is not hot code and needs to trigger just-in-time compilation is called hotspot detection.

One of the most important features of the Java language: cross-platform use is due to the existence of the JVM.

3.JVM MEMORY structure of the VM

The JVM virtual machine divides its memory into program counters, virtual machine stacks, local method stacks, Java heaps, and method areas.

If we look at the picture above, when the file is loaded, it becomes a JVM process and the bytecode file runs inside the JVM process. When a thread is started, it occupies an area of memory, independent of each other. Partition only considers thread safety, thread sharing is not safe, there are threads competing for resources.

Ps: there are friends who want to learn but have no learning materials can look at the Java300 set, very suitable for Java white learning, video content is easy to understand, easy to learn!

4. Do you really know the method zone?

The method area is actually a shared memory area, and the information reflected in Java source files is called class information. Java source files are compiled into binary files and loaded into the JVM for execution. For example, if you want to retrieve a class object from the student. class, the class object is stored in the method area, and you actually get a reference to the method area.

To mention more, the method area also has garbage collection, but the recycling rate is low, mainly for constant pool collection, and type offload. If the method area cannot meet the memory requirements, the SYSTEM reports OOM.

Draw heap memory in the shared area.

Student stu = new Student();

New objects are stored in the heap, stu is stored in the stack, and the heap mentioned here is the heap area.

For most applications, this area is the largest chunk of memory managed by the JVM. Thread sharing, mainly to store object instances and arrays. Multiple Thread Local Allocation buffers (TLabs) are allocated internally. It can be physically discontinuous, but logically continuous.

Think about what objects are stored in the heap. Layout of Java objects in memory?

Int num = 1; Instead of storing the name of num, store 1. Then we store marsLee, not the name, but the address of the reference heap.

Meta-information is stored in the method area, and meta-information (templates) is instantiated in the heap area. Both static variables and methods have method sections. A store object stores only the value of the instance object, not the name.

How do we identify the object?

An object header containing a Mark Word and a Klass Pointer

Klass Pointer a Pointer to an object’s class metadata that the virtual machine uses to determine which class instance the object is.

Mark Word, used to store the runtime data of the object itself, such as HashCode, GC generation age, lock status flags, locks held by threads, bias thread ids, bias timestamps, and so on.

5. To summarize

Object heap area, thread safety? Not safe. It is unsafe for multiple threads to operate on an object unless it is read-only.

Is this static field safe when multiple threads access the Student object? Not safe.

6.Java Learning Materials Sharing:

Java video: Java300 sets! Learning Java, let learning become a kind of enjoyment