preface

What is the run-time data area? Take a look at the following figure, which is the focus of today’s discussion.

1. Program counter (register)

An indicator of the bytecode line number executed by the current thread

The bytecode interpreter relies on counter control to complete its work

By performing the thread line number record, let the thread in turn switch between each thread counter does not affect each other

Threads are private and have the same life cycle as threads, living when the JVM starts and dying when the JVM shuts down

When a thread executes a Java method, it records the address of the virtual machine bytecode instruction that it is executing

When thread executes Nativan method, counter record is null (Undefined)

Only the Java Virtual Machine specification does not specify any areas of OutOfMemoryError cases

In it, many don’t understand it doesn’t matter, we studied a multi-threaded, there are two threads, in which a thread can suspend, let the other threads to run, and then get the CPU resources such as yourself, can start where suspend operation again, so why are able to remember the location of the suspension, it is depend on the program counter, through this example, Java backend interview 问 题 100+ answer analysis + notes

2. Local method stack

I don’t know if you have read the source code, read should know, a lot of algorithms or a function of the implementation, are Java encapsulated in the local method, the program directly by calling the local method on the line, the local method stack is used to store this method, The code that does this might be C or C++, but not necessarily Java.

The first two are not the focus of our study, the next three are.

3. Virtual machine stack

The virtual stack describes the in-memory model of Java method execution: When each method is executed, a stack frame will be created to store the information of local variation table, operand table, dynamic connection, method exit and so on. The process of each method from invocation to completion of execution corresponds to the process of a stack frame in the virtual machine stack from loading to unloading. How to understand this sentence? For example, when executing a class with a main method, a stack frame will be created for the main method and then added to the virtual machine stack. The stack frame will hold various local variables, object references and other things in the main method. As shown in figure

When another method is called in the main method, another method frame is added to the virtual machine stack, and when that method is called, the stack pops, and then main is at the top of the stack, and the execution continues until the end, and then the main method pops, and the program ends. In short, the virtual machine stack is a lot of stack frame on the stack, stack frame stored in the city some variable names and other things, so we usually say that the stack stored in some local variables, because local variables are in the method. In the stack frame, that’s what it says.

The above three are not shared by threads, that is, this part of memory, each thread is unique, will not let other threads access, the next two are shared by threads, which will cause thread safety problems.

4, heap

An area of memory shared by all threads. The largest chunk of memory managed by the Java virtual machine, because the sole purpose of this memory area is to hold object instances. Almost every instance of an object allocates memory here, which is usually called a new object, and that object creates a chunk of memory in the heap to hold some information about the object, such as properties. The heap is also the main area managed by the garbage collector. This is often referred to as the “GC heap “, and the virtual machine’s garbage collection mechanism will be covered in the next article. Most of the degrees referred to by locally referenced variables stored in the stack described in the previous point are stored in the heap.

The method area and its run-time constant pool

Like the heap, an area of memory shared by threads that stores class information that has been loaded by the virtual machine, constants, static variables, and compiler compiled code (that is, bytecode files). Int I =3, int I =3, int I =3, int I =3, int I =3, int I =3, int I =3, int I =3 These are stored in a place called a constant pool (which is in a bytecode file), and the contents of that constant pool are put into the runtime constant pool when the class is loaded into the method area. It is important to note here that constant pools and constant pools at runtime are not to be confused. Constant pools also exist in bytecode files, which will be covered in more detail in a later section. All you need to know is that there is a pool of runtime constants in the method area, where they are stored. Also, the runtime constant pool does not necessarily need to fetch constants from the bytecode constant pool. It is possible to put new constants into the pool during program execution, such as the string.intern () method, which does this: The method area looks for the value in the runtime constant pool, returns a reference to the value if it does, and adds it to the runtime constant pool if it does not.

The heap, the virtual machine stack, and the method area are used most often in normal analysis.

For example: look at the following program, and then draw a memory analysis diagram

The most important thing is to see my analysis process, this diagram because to show the dynamic stack cannot be drawn, so it can only be drawn that way.

Java will change to Demo1_car.class. Add demo1_car. class to the method area

2. When the main method is encountered, create a stack frame, add it to the vm stack, and start running the program in the main method

3, Car c1 = new Car(); It’s the first time you’ve encountered the Car class, so compile car.java as a Car. Class file and add it to the methods section, as in step 1. And then new Car(). Create an area in the heap to hold the created instance object at address 0X001. There are two values for color and num. The default values are null and 0

4. Select color and num from c1.

5, call the run method, and then create a stack frame, used to hold the local variables in the run method, into the virtual machine stack, run method printed a sentence, after the end, the stack frame out of the virtual machine stack. The only stack frame left is the main method

6. Next, a Car object is created, so another chunk of memory is created in the heap, and it is the same as before.

Now that the analysis is done, you should have a general idea of the heap, virtual stack, and method area in mind. Note that the name of this method area does not just hold methods, it can hold many things.

This is just a simple analysis, can be more specific, 1, create objects, create memory in the heap how to allocate memory? 2. How do object references find instances of objects we have in the heap? Let’s use these two questions to deepen our understanding.

1. How is memory allocated when creating objects and allocating memory in the heap?

Two ways: pointer collisions and free lists. Which one we use depends on what we use in our virtual machine.

Pointer collision: suppose the Java heap memory is absolutely neat, put aside all the memory used by degrees, within the free storage, on the other side stood a pointer as a cut-off point between indicator, allocated memory which is just the pointer to the free space to move over there a equals the size of the object, for example, the scheme is called a pointer

Free list: Have a list of which memory blocks are useful, find a chunk of the list that is large enough to be allocated to object instances at allocation time, and update the list of records. This is called the free list

2. How do object references find instances of objects we have in the heap?

This problem can also be called an object access location problem, and it can be done in two ways. Handle and direct pointer access. Let me draw a couple of pictures.

Handle access: A block of memory is allocated to the Java heap as a handle pool. Reference variables store the handle address of the object, and the handle contains the specific address information of the object instance data and the type data respectively

There is a reference variable in the stack that points to the address of a handle in the handle pool. This handle contains two addresses, one for object instance data and one for object type data (this is in the method area because the class bytecode files are in the method area).

Direct pointer access: The reference variable stores the address of the object directly, as shown in the figure

In the heap, there is no handle pool, pointing directly to the address of the object, which contains the address of the object type data.

The difference between: The main advantage of using handles for access is that reference variables store stable handle addresses. When objects are moved (which is common in garbage collection), the instance data pointer in the handle changes, but the address to which the reference variable points does not change. The biggest advantage of using direct pointer access is faster, saving the time cost of a pointer location, but when the object is moved, and need to change the address of the reference variable. In the example we analyzed above, this is the direct pointer access approach used.