JAVA advanced interview Summary-JVM, java interview-jvm

Source: Internet
Author: User

JAVA advanced interview Summary-JVM, java interview-jvm

1. Sun HotSpot VM is a self-embedded Virtual Machine in JDK and Open JDK. It is also the most widely used Java virtual machine.

2. JVM Memory Distribution

Program counter: it is a small memory space and can be seen as the row number indicator of the bytecode executed by the current thread. The basic functions of the program, such as branch, loop, jump, exception handling, and thread recovery, must depend on this counter. Multithreading is implemented by switching threads in turn and allocating the execution time of the processor. Therefore, this region is the private memory of the thread.

Virtual Machine Stack: describes the Memory Model of Java method execution. It is used to store local variable tables, operand stacks, dynamic links, and method exits.

Heap: it is the largest part of memory managed by the Java Virtual Machine. the Java heap is a memory area shared by all threads. It is created and stored in the instance when the virtual machine is started, it is also the main management of the garbage collector.

Method Area: used to store data such as class information, constants, static variables, and compiled code loaded by virtual machines. HotSVM also performs GC for this region, mainly including constant collection and class

3. JVM Memory Allocation Policy

The memory allocation of objects is distributed in the Java stack in a large way.

In most cases, objects are allocated in the new generation Eden area. When the Eden area does not have enough space for allocation, the virtual machine will initiate a Minor GC.

In most cases, large objects directly enter the old age. virtual machines provide parameters to define the threshold value of large objects. objects exceeding the threshold value will directly enter the old age.

Objects that remain alive after multiple Minor GC operations (long-lived objects) will enter the old age. The virtual machine provides parameters that allow you to set thresholds.

4. JVM garbage collection Algorithm

Mark-clear algorithm: first, mark all objects to be recycled. After marking, all marked objects are recycled.

Replication algorithm: divides the available memory into two equal-size blocks by capacity, and only one of them is used at a time. When one piece of memory is used up, it will be stored in another part, and the used memory space will be cleared once.

Tag-sorting algorithm: The tag process is the same as the "tag-clear" algorithm, but the subsequent steps do not directly clean the recyclable objects, but move the end of the object, then, the memory outside the end boundary is cleared directly.

Generational collection algorithms: Generally, Java heap is divided into the new generation and the old generation, and the most appropriate collection algorithms are used according to the characteristics of each generation. A large number of objects are found in the new generation, and the replication algorithm is used. In the old age, because the object has a high survival rate, you must use the "tag-clean" or "tag-clean" algorithm to recycle objects.

5. Garbage Collector

Serial COLLECTOR: A single-threaded collector that only uses one CPU or one collection thread to collect garbage. When collecting garbage, all other working threads must be suspended, until the collection ends.

ParNew COLLECTOR: a multi-threaded version of the Serial collector. In addition to using multiple threads for garbage collection, the other operations are exactly the same as those of the Serial collector.

Cms collector: it is a collector designed to obtain the minimum recovery pause time. The process is divided into the following four steps:

Initial tag

Concurrent tag

Remark

Concurrent cleanup

6. Common JVM startup parameters

-Xms/-Xmx-initial heap size/maximum heap size

-Xmn-size of the young generation in the heap

-XX:-DisableExplicitGC-Disable System. gc ().

-XX: + PrintGCDetails-print GC details

-XX: + PrintGCDateStamps-print the GC operation Timestamp

-XX: NewSize/XX: MaxNewSize-set the size of the new generation/the maximum size of the New Generation

-XX: NewRatio-you can set the ratio of the old generation to the new generation.

-XX: PrintTenuringDistribution-sets the age distribution of objects in the survivor's paradise after each young generation GC.

-XX: InitialTenuringThreshold/-XX: MaxTenuringThreshold: Set the initial value and maximum value of the threshold value in the old age.

-XX: target1_vorratio: sets the target usage rate in the surviving zone.

7. JAVA class Lifecycle

Java class starts from being loaded into the VM memory and ends with detaching the memory. Its Entire Lifecycle includes seven stages: Loading, verification, preparation, parsing, initialization, use, and detaching.

8. JVM class loading

Bootstrap: A Class Loader implemented using local code. It is responsible for loading the Class Libraries under <Java_Runtime_Home>/lib to the memory (such as rt. jar ). Since the bootstrap loader involves the local implementation details of the virtual machine, developers cannot directly obtain the reference of the bootstrap loader, so they cannot directly perform operations through the reference.

The standard Extension class loader is composed of Sun's ExtClassLoader (sun. misc. launcher $ ExtClassLoader) implements Java_Runtime_Home>/lib/extjava. ext. the class library in the specified location of dir is loaded into the memory. Developers can directly use the standard extension class loader.

System Class Loader: It is implemented by Sun's AppClassLoader (sun. misc. Launcher $ AppClassLoader. The class library specified in the path (CLASSPATH) is loaded into the memory. Developers can directly use the system class

Description of the parent-class delegation mechanism: when a specific class loader receives a request for loading classes, it first delegates the loading task to the parent class loader, recursively returning it, if the parent class loader can complete the class loading task, it will return a successful result. Only when the parent class loader cannot complete the loading task can it load it by itself.

9. JVM Optimization

View heap space allocation (distribution of young, old, and persistent)

Garbage Collection Monitoring (monitoring of garbage collection over a long period of time)

Thread Information Monitoring: Number of system threads

Thread status monitoring: the status of each thread

Thread details: view the internal running status of the thread and check the deadlock

CPU hotspot: check which methods occupy a large amount of CPU time.

Memory hotspot: check which objects have the largest number of objects in the system

10. Performance Optimization

(The pursuit of efficiency and scientific optimization does not depend on luck)


Welfare has come to share this article to the circle of friends to send your JVM learning materials to the background editor!


Long press the QR code to follow us



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.