Face questions from an in-depth understanding of java virtual machine memory structure

I remember just graduated from university, in order to cope with the interview, crazy online brushed JAVA questions, and many of them by rote. The proper way in which the interview questions, impressed me very deeply, there is a giant of the interviewer, along This question has been asked down, and asked to knowledge java virtual machine, and finally I asked to live.
I was face is this:

Later, I had the opportunity to interview others, and also in accordance with his ideas to come forward questions, many have been working for 2 years, programmers, results and I was, like, all lost in the java virtual machine knowledge.

We look at the face questions:

String str1 = "hello Alunbar";
String str2 = new String(str1);

Several objects are created?

Explanation given online is to create two objects, str1 objects in the constant pool, str2 objects on the heap.

The following is a dialogue of the interviewer and me.
Interviewer: The above code creates several objects?
I: 2.
Interviewer: Why is it two?
I: str1 objects in the constant pool, str2 objects on the heap. With "=" equal sign create a String object first look in a string constant pool already exists string object, there is a direct reference to the return address, or create a string object and returns a reference address.

Interviewer: Why create a string object constant pool?
I:. . . I thought about it for half a minute, do not know the answer embarrassing.
Interviewer: Tell me about the structure of memory jvm virtual machine.
I:. . . I once again undaunted, the situation was very embarrassing.

After the end of the interview, I went back to frantically find information, understand the relevant knowledge jvm virtual machine.

This is my first interview, impressed me very deeply.

Let me talk about two issues interviewer.
1, why would create a string object constant pool.
2, java virtual machine's memory structure.

First look at the first question.
Why create a string object constant pool?

String in all programming languages ​​are the most common type, other data types can be converted to a string type, like int, long, etc. are the basic data types and String can be converted to each other. To improve the efficiency of the use of string, JVM virtual machine set up a special constant pool of a memory for storing basic data types of the object, the object can be shared constant pool to each other, of course, include the String.

We will generally be stored in the constant pool of the string to a string constant pool. String constant pool there will be a lot of already created a string object, because the String class is final modified, its value can not be changed once created, so we do not worry about String objects share and bring chaos program.

We look at a section of the code:

String s1 = "Hello";
String s2 = "Hello";

This code creates only one object, s1 and s2 are the same object. According to the above interpretation, java String s1 = "Hello"this line of code will first look for the string constant pool Hello object is not found, then create a Hello object and returns a reference to s1. java String s2 = "Hello"This line of code, but also to go to find a string constant pool Hello object, found that already exists, the process directly returns to s2. Thus s1 and s2 are the same object.

Then talk about using the new create a string object.
Create a new string object through, will open in a new heap memory space, storage string String objects, using new methods will generate a new string object, regardless of whether the content of a string of consistent use new to create a string create a string object exists heap, heap object will be recycled, and use the "=" create a string object is stored in the constant pool will not be recovered, so we recommend using the "=" approach, to avoid unnecessary java object creation and destruction overhead.

We look at the following chart when the memory structure to create a string object:

s1 and s2 are the "=" string objects that are created, their memory addresses are the same, s3 embodiment is created using the new string objects, s3 and s1, s2 memory address is not the same.

Now and then look at the second question.

Memory structure java virtual machine
virtual machine memory structure is a very complex problem, there can only talk about a major speaking role of each memory area.

java virtual machine by a class loader, a data area, and the runtime execution engine configuration. As shown below:

Usually we say the java virtual machine memory structure is talking about run-time data area.

java virtual machine java program execution, memory will be divided into several areas: program counter, method area, the virtual machine stack, native method stacks, heap.

Among them, the method and heap area are shared thread, the thread does not share the program counter, the virtual machine stack, native method stacks.

1, the program counter
as long as the learned assembly language, this program counters are easy to understand, is recorded under a bytecode instructions to be executed.

We know that knowledge of the operating system when you start a program, it will create a process, and therefore in the implementation of java program, will create a process, a process that is java virtual machine.

A process composed of a plurality of threads, at any one time, java virtual machine can execute only one thread of instructions.

java virtual machine by reading a program counter to determine which one thread of the thread needs to perform basic functions, such as cycling, reading the database, jump, exception handling, thread recovery.

Therefore, each thread of the program counter are independent of each other, affect each other.

2, java virtual machine stack
is what we often say java stack, in the implementation method, creates a stack frame in java stack, local variables table for storing information, operand stack, the method exports.

boolean, char and other basic types of data will be stored in the local variable table you need to perform the method, object reference like. Local variable table size has been determined during the code compilation. java thread stack is private.

When you create a thread synchronization to create java stack, thread end, java stack also destroyed, release the memory occupied.

3, native method stacks
and stacks similar to the java virtual machine functions, some of the java virtual machine virtual machine stack and native method stacks merge. Native method stacks of virtual machines are primarily Native method of providing service.

4, java heap
virtual machine in the largest area of memory, the virtual machine starts to create, mainly used to store an object instance, this memory area is shared by all threads. Objects in this region, all the threads can be accessed.

This area is also subject java virtual machine management focus, and when this area of ​​the object is not referenced, reaching recycling standards, will be recycled java garbage collector to release the contents of the space occupied.

java heap into the new generation and the old era, the Cenozoic is divided into Eden space, From Survivor To Survivor space and space.

When you create an object using the new operator, this will open up an area in memory used to store objects.

Mentioned above java String str1 = new String("Hello")to create a string, it will open up in a java heap memory is used to store str1 objects.

5. The method area
the main storage area is loaded method of virtual machine class information, constants, variables and other static data, we will be in this area of memory called the permanent generation memory of this area will not be recovered.
Like java heap and method area, shared by all threads.

The method area contains a runtime constant pool, the above-mentioned java String str = "Hello"created string is created at runtime constant pool "Hello" object.

Summary:
1, two ways to create differences in a string object.
2, the role of java virtual machine memory area.

Guess you like

Origin www.cnblogs.com/airnew/p/11614031.html