[Java interview eight part essay] JVM

1. Let's talk about the JVM memory model (runtime data area)

The JVM memory model is divided into two parts: thread sharing and thread private

JDK1. After 8, the method area is replaced by the meta space.

2. Object creation process

3. Memory layout of objects

4. How does reference in stack memory access variables in heap memory

5. JVM garbage collection overview

Which memory should be garbage collected

The thread private memory space does not need garbage collection, because when the method ends or the thread terminates, the memory will be recycled naturally. The main battlefield of garbage collection is the heap, and the main target is the objects allocated in the heap. This part of the memory allocation and recycling is dynamic.

What objects need to be recycled?

Dead objects need to be recycled. Whether an object survives or not is inseparable from "reference". That is, objects that are not referenced or pointed to by pointers will be recycled.

Detailed explanation of references in Java

How does the JVM determine that an object is dead (without a reference)

6. Waste recycling in method area

Garbage collection in the method area mainly recycles two parts: discarded constants and class information that is no longer used.

How to judge a constant as an obsolete constant

If the string "ABC" exists in the constant pool, if no string object currently references the string constant, it means that the constant "ABC" is an abandoned constant. If memory recycling occurs at this time and it is necessary, "ABC" will be cleaned out of the constant pool by the system.

How to judge a class as useless

Virtual machines can recycle useless classes that meet the above three conditions. What we say here is only "yes", not that they will be recycled if they are not used like objects.

7. Garbage collection algorithm

Three hypotheses and why the heap should be divided into generations

According to the empirical criteria of the actual situation of most programs, we find that the objects in the heap have the following characteristics:

According to these three hypotheses, the collector should divide the Java heap into different areas, and then allocate the recycled objects to different areas for storage according to their age (that is, the number of times the object has survived the garbage collection process). Different generations of heaps use different recycling algorithms to achieve maximum efficiency, so as to improve the efficiency of garbage collection.

In the new generation, each time we recycle, we only focus on how to keep a small number of objects alive rather than marking a large number of objects to be recycled, so we can recycle a large amount of space at a low cost. In short, the new generation dies quickly. We only need to pay attention to those who are not dead.

In the older generation, virtual machines can reclaim this area with a lower frequency, which takes into account the time overhead of garbage collection and the effective utilization of memory space. In short, the old generation can't die, so it's only recycled once for a long time.

This is why the heap should be divided into generations: choose the most appropriate GC algorithm.

Mark clear algorithm

The algorithm is divided into two stages: marking and clearing: first, mark all objects that do not need to be recycled, and uniformly recycle the unmarked objects after marking. It can also be reversed. Marking process is the process of determining whether an object belongs to garbage.

The most basic collection algorithm and subsequent collection algorithms are based on its improvement.

Disadvantages:

Mark copy algorithm

Divide the available memory into two blocks of equal size according to capacity, and use only one of them at a time. When this block of memory runs out, copy the living objects to another block, and then clean up all the used memory space at once.

This algorithm is based on Hypothesis 1: most objects can't survive the first round of garbage collection. Therefore, replication is only a small number of operations, and recycling is finished.

Disadvantages:

Marking sorting algorithm

The mark and tidy algorithm is a "mobile" mark and clear algorithm, which first moves the living objects to one side of the memory, and then empties the memory outside the end boundary.

In fact, this algorithm also has disadvantages. For the old generation area, the object survival rate is high, so the moving code is also high. But not moving will cause the problem of memory fragmentation. It's not hard to have the best of both worlds.

There is also a "gentle" way. First, use the mark clear algorithm to recycle garbage, and then use the mark clear algorithm to deal with memory fragments when there are really many memory fragments.

Generational collection algorithm

At present, the garbage collection of virtual machines adopts generational collection algorithm. This algorithm has no new idea, but divides the memory into several blocks according to the different life cycle of objects. Generally, the Java heap is divided into the new generation and the old generation, so that we can choose the appropriate garbage collection algorithm according to the characteristics of each era. We've talked about all this above.

For example, in the new generation, a large number of objects will die in each collection, so you can choose the replication algorithm. You can complete each garbage collection by paying only a small amount of object replication cost. The survival probability of objects in the old age is relatively high, and there is no additional space to allocate and guarantee it, so we must choose the "mark clear" or "mark tidy" algorithm for garbage collection.

8. Common garbage collectors

Garbage collectors are generally divided into four categories:

Hotspot implements many garbage collectors, which can be used together. Generally, a collector is selected for the new generation and a collector is selected for the old generation. The connection indicates that the two recyclers can be adapted.

How to view the default garbage collector

java -XX:+PrintCommandLineFlags -version

Parallel scavnge + parallel old is used by default

Minor GC, major CG and full GC

Serial collector

Serial Old

ParNew(Parallel New)

Parallel Scavenge

Parallel Old

CMS

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>