Java Virtual Machine Basics

Source: Internet
Author: User

Write in front

Before the boss let me do some outsourcing interview, my question is very simple:

    1. Describe the more interesting problems that have been solved in the work.
    2. HashMap points to be aware of in use.

The first question is to understand the other side of the project experience, the second question is whether to know some details, such as HashMap is thread unsafe, with HashMap to do cache can lead to memory leaks, self-feeling problem design can also: d~ But when I saw the other colleague's topic, tears collapsed:

    1. design mode xxx
    2. Garbage collection XXX

Rub, how to feel this problem I also do not ...

The virtual machine feels like the operating system, the compiler: very tall. But Java program ran on the above, encountered problems still have to go to troubleshoot, performance does not have to optimize, basic knowledge or need!

Memory management

The Java Virtual machine performs the process of dividing the memory that it manages into several different data regions, roughly as follows:

The functions of each section are as follows:

The larger piece of content in the Memory Management Section is the GC (garbage collection), the so-called garbage collection that reclaims the garbage consumed memory. So the first question: What is rubbish?

    1. Reference counting algorithm: An object that has been referenced to 0 times.
    2. Root Search algorithm: Objects that are not found from GC roots along references.

references are mentioned here, and after JDK 1.2 Java has extended the concept of references, the second question: What types of references are there?

    1. Strong reference: Object o = new Object () This is a strong reference.
    2. Weak references: Useful but not necessary, are recycled before oom.
    3. Soft references: Weaker references are recycled at the next GC.
    4. Virtual reference: The weakest and only function is to be notified when an object is recycled.

Only strong references here can affect the life cycle of an object. In the process of virtual machine development, many garbage collection algorithms have evolved, such as:

    1. Tag-Purge algorithm
    2. Replication Algorithms
    3. Tagging-sorting algorithms
    4. Generational collection Algorithms

The actual use of the collector is a combination of these algorithms, such as the memory seen from the VISUALVM is such (need to understand how each part is compatible with each other):

On the whole, it is a generational collection algorithm, and S0 and S1 can be regarded as the marker-collation algorithm. So the third question: What is the implementation process of the common CMS garbage collector?

    1. Initial tag: GC roots directly associated object.
    2. Concurrency token: Root tracing.
    3. Re-tagging: Fixed a change in markup due to program operation.
    4. Concurrent cleanup

As shown in the following examples:

You can see that stop the world is only needed for initial markup and re-tagging, and the rest is executed with the user thread, and don't assume that this is perfect, and that the process of executing in parallel consumes some CPU resources.

Code execution

The Java source to the JVM must not be executed, you need to compile a class file with Javac first, then the first question:What is the structure of the class file?

    • Constant pool
    • Access flags
    • Class index, parent class index, and interface index
    • Field table
    • Method table
    • property sheet

The virtual machine specification does not specify when the class is to be loaded, but specifies that initialization is required when encountering new, reflection, parent, Main . The life cycle of the entire class is as follows:

To load classes in a virtual machine via ClassLoader, this place needs to be understood:

    • Whether the two classes are the same, you need to determine whether the ClassLoader is the same in addition to the class name.
    • Parental delegation mode is not a mandatory constraint.

After the class loading is complete, you can start execution, and everything related to thread operation is placed in the stack frame, which is structured as follows:

Which method to call in the execution is a headache and needs to be addressed:

    • Static Dispatch: A method with the same name and different parameter types.
    • Dynamic Dispatch: A Method of replication in succession.

The instructions in the bytecode are stack-based operations, such as the completion of the calculation, the corresponding instructions are as follows:

iconst_1 // 将常量1压入栈iconst_1iadd // 把栈顶的两个值相加并出栈,然后把结果放回栈istore_0 // 将栈顶的值放到局部变量表第0个Solt

The benefit of interpreting execution is that it is fast to start after downloading, but the certainty is also obvious: slow running. JIT is precisely used to solve this problem, the ability to call the method, multiple execution of the loop body compiled cost code.

Optimization is a very interesting topic, remember to participate in a game when the Gcc-o3 compiled code to the printf () did not output. The more common optimization methods in the JIT are:

Program execution is bound to involve memory operations, which are defined in Java with eight actions to complete:

Here it is necessary to talk about the role of volatile , when used to understand the following two can be:

    • Ensure that the variable is visible to all threads.
    • Prohibit command rearrangement optimization.

If all the operations in Java require the programmer to control, there will be a lot of duplication of code, and writing is very tired, then we can use the principle of the first occurrence to determine whether there is a conflict in parallel two operations:

    • Program Order rules: single-threaded in accordance with the sequence of program writing.
    • Pipe lock rule: unlock must be before lock.
    • Volatile variable rule: The write operation precedes the read operation.
    • Thread Start rule: Thread.Start () Any other method that precedes the thread.
    • Thread termination rule: All operations in a thread are preceded by termination detection for this thread.
    • Thread Break rule: interrupt () is preceded by interrupt detection.
    • Object Finalization rule: The initialization of an object is done before its finalize () method.
    • Pass rule: If a precedes B and B before C, then a precedes c.

The underlying implementation of thread is cumbersome, but at the very least you should know how the thread's state is converted:

Finally, the common way of synchronization is synchronized or Aqs of the various implementations, here is not speaking, because each is enough to write a large article.

Java Virtual Machine Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.