Hello, I’m Li Guo, your JVM lecturer. This article is from The Education column “Java Virtual Machine in Simple Terms”.


Welcome to class 05, where we’ve talked more than once about a heap. A heap is a large pool of objects. A large number of object instances are managed in this object pool.


Some of the objects in the pool have very deep levels of reference. A frequently invoked interface generates objects per second at a significant rate. The relationships between objects form a vast web. While Java has always fostered an atmosphere of unlimited memory, objects can’t just grow and grow, so garbage collection is required.


How does the JVM determine which objects should be reclaimed? Which ones should be maintained?


In ancient times, there were nine ethnic groups to be executed. Refers to when some people commit serious crimes, when the emperor kills one person is not enough to calm the inner anger, will have joint liability to relatives and friends. The nine clans need to be traced first to a common ancestor and then down the chain. The same goes for garbage collection on the heap. Let’s take a closer look at how garbage collection works in the JVM.

The JVM’s GC actions are not controlled by the program and are triggered automatically when conditions are met.


When a GC occurs, the JVM can always find an ancestor that references an object. At the end of the day, if the ancestor is found to be dead, they are all removed. And the ancestors who were able to dodge the garbage collection were special: they were called GC Roots.


Tracing and searching down from GC Roots produces a Chain called the Reference Chain. When an object can’t relate to any GC Root, it is ruthlessly killed.


Obj5, Obj6, and Obj7, as shown in the figure, are destroyed when GC occurs because they cannot be associated with GC Root.



This article is from The Education column “Java Virtual Machine in Simple Terms”.

Recycling is done around GC Roots. It is also the source of many memory leaks because other references have no such rights.

So, what kind of object is GC Root? It’s not what kind of object it is, it’s where it is.

Which GC Roots are available

GC Roots is a set of must-active references. In layman’s terms, it is a potentially used object that a program can then access by direct or indirect reference.

GC Roots include:

  • In a Java thread, reference type parameters, local variables, temporary values, and so on for all methods currently being called. That is, the various references associated with our stack frame.
  • All currently loaded Java classes.
  • A Java class reference type static variable.
  • A reference type constant (String or Class) in the runtime constant pool.
  • The JVM internal data structures of some references, such as the sun. The JVM. Hotspot. The memory. The Universe.
  • Monitor objects for synchronization, such as objects whose wait() method is called.
  • JNI handles include global handles and Local handles.
These GC Roots can be divided into three general categories, and the following is easier to remember:

  • Various references to active threads.
  • Class reference to a static variable.
  • JNI references.

There are two caveats:

  • We are talking about active references here, not objects, which cannot be used as GC Roots.
  • The GC process is to find all live objects and consider the rest of the space “useless”; Instead of finding all the dead objects and reclaiming the space they take up. Therefore, even if the JVM heap is very large, the tracing GC can be very fast.

Reference level

The following interview question is much more interesting: if you can find the object of Reference Chain, will it definitely survive?

I often ask these questions in interviews, like “What’s the use of weak references?” It strikes me that even some Java engineers who have been working for many years are half-baked about this problem and miss opportunities.


An object’s reference to another object, depending on how strong the relationship is, may break at one link in the chain.


This reference relationship can be further divided depending on how the chain behaves when GC occurs.

Their relationship can be divided into strong reference, soft reference, weak reference, virtual reference and so on.



Typical OOM Scenario

The full name Of OOM is Out Of Memory, so which Of our Memory areas will be OOM? We can look at the color part of the memory partition diagram.

You can see that OOM overflow is possible in all areas except the application counter. But the most common is in the heap.

So what exactly causes OOM? There are several reasons:

  • The memory capacity is too small and needs to be expanded or the heap size needs to be adjusted.
  • Incorrect reference, memory leak. The relationship with GC Roots was not cut off in time. For example, threads in a thread pool forget to clean up the contents of a ThreadLocal in case of reuse.
  • The range of the interface is not verified, and the external parameters are out of the range. Procedure For example, the number of items per page in a database query.
  • Unlimited use of out-of-heap memory. In a more serious case, the operating system may run out of memory.
A typical memory leak scenario occurs when an object does not free its reference in time. For example, a local variable is referenced by an external static collection.

When you’re writing regular code, be aware of this and don’t refer to objects around for convenience. Even if references are made, manually clean them up when appropriate. As for the root cause investigation of this part, we will introduce it in detail in practical courses.

summary

You’ll notice that GC Roots is technically called accessibility analysis. In addition, there is a method called reference counting, which is often used to determine the survival of objects.

Because of the vulnerability of loop dependency, none of the major JVMS implement GC using reference counting, so let’s just take a look. Reference counting maintains a counter counter in the head of the object that is referenced by +1 at a time, and the reference invalidation count is -1. When the counter is 0, it is considered invalid. You can forget about reference counting now.

In this lesson, we went over what GC Roots includes. HostSpot adopts the tracing method for GC. The speed of memory collection is related to the number of objects in the living state.

Discussed several typical OOM scenarios that you may not have a clear idea of right now. In the rest of the class, we’ll go through several common garbage collection algorithms and then break down each of these OOM scenarios.

Next time we’ll take a closer look at recycling, so be sure to come to class on time. See you next time.



This article is from The Education column “Java Virtual Machine in Plain English”.



Copyright notice: The copyright of this article belongs to Pull hook education and the columnist. Any media, website or individual shall not be reproduced, linked, reposted or otherwise copied and published/published without the authorization of this agreement, the offender shall be corrected.