“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

  • About the author: Inspirational not bald a CURD Java migrant worker
  • About the article: the following content is simply a summary that the author thinks is often encountered in the interview. At the same time, some questions that the author encountered in the interview will be interspersed as a record. The * marks are the ones that the author thinks are more important subjectively.

This article mainly records the author sorted out some questions about the JVM interview, hope to help you, also JVM series of final work.

When does the JVM trigger garbage collection

  1. The Eden region of the young generation does not fit, triggering the Minor GC
  2. Before the Minor GC, the old age guarantee triggers, and the old age less triggers the Full GC
  3. After Minor GC, through dynamic age judgment, insufficient space in Suvivor area and age promotion, the object enters the old age, and the old age space is insufficient, triggering Full GC
  4. Space to use the old s meet specifications – XX: CMSInitiatingOccupancyFraction setting threshold, trigger a Full GC

Is GC always done before an OutOfMemoryError is thrown? *

  • Typically, before an OutOfMemoryError is thrown, the garbage collector is triggered to clean up as much space as it can.

  • However, the garbage collector is not triggered in every case. For example, if we allocate a very large object, like a very large array that exceeds the maximum size of the heap, the JVM can determine that garbage collection is not going to solve the problem, so it simply throws an OutOfMemoryError.

Have you read the card form?

Card tables are used to avoid global scanning of older objects. In the young GC, there is no need to scan to the old age

  • Each small area of heap memory forms a card page, which is a collection of card pages, and the JVM marks a card page as dirty when it determines that it has cross-generation references to existing objects (older objects hold references to younger objects).
  • After knowing the card table, each Minor GC only needs to go to the card table to find the dirty page and add it to the GC Root instead of going through the entire old age object.

Why is G1 recommended for large memory (over 32GB) and not for small memory? *

The G1 is suitable for scenarios with large heaps (16 GB or 32 GB) or services that do not have high latency

When ParNew + CMS is faced with a large heap, the STW time of each collection is relatively long and can be used in small memory, but the effect is not as obvious as large memory

JVM young generation garbage collection algorithm, when objects are moved to old generation **

  • The new generation uses replication algorithms
  • The old days used mark-tidy or mark-clean algorithms
  1. Large object generation is stored directly in the old age
  2. When transferring to Suvivor area, the space is insufficient, and the old age guarantee mechanism is adopted to enter the old age
  3. Age promotion or dynamic age judgment into the old age

How do I check the JVM

  1. The company’s monitoring platform
  2. You can log through GC
  3. Analysis using the jstat command
  4. Analysis by exporting the JMAP memory snapshot

How to check and handle the online system after OOM

First of all, the first time to recover, to avoid greater impact

  1. First look at the log file exception information, determine metaspace, stack, heap where memory overflow
  2. Generally speaking, the OOM site information is exported to the local PC and checked using the MAT tool
  3. One is the code development caused by this time to fix the optimization generated OOM code

Please talk about memory leaks

A memory leak occurs when objects are no longer used by the program, but the GC cannot reclaim them

Disclosure:

  1. Static collection class

    Static collection classes, such as HashMap, LinkedList, and so on. If these containers are static, their lifetime is consistent with that of a JVM program, and objects in the container cannot be released until the program ends, resulting in a memory leak. Simply put, a long-life object holds a reference to a short-life object, which cannot be reclaimed because the long-life object holds a reference to it, even though the short-life object is no longer in use.

    public class Test { static List list = new ArrayList(); Public void oomTests() {Object obj = new Object(); list.add(obj); }}Copy the code
  2. The singleton pattern

    The singleton pattern causes memory leaks in a similar way to static collections. Because of the static nature of the singleton, its lifetime is as long as that of the JVM, so if a singleton holds a reference to an external object, that external object will not be recycled, causing a memory leak.

  3. The inner class holds the outer class

    An inner class holds an outer class if a method on an instance object of an outer class returns an instance object of an inner class. The inner class object is referenced for a long time, and even though the outer class instance object is no longer in use, because the inner class holds the instance object of the outer class, the outer class object will not be garbage collected, which can also cause a memory leak.

  4. All kinds of connections, such as: MySql connection, Es connection

    In the process of operation on the database, the first need to establish a connection with the database, when no longer used, you need to call the close method to release the connection with the database. Only after the connection is closed will the garbage collector reclaim the corresponding object. Otherwise, if a Connection, Statement, or ResultSet is not explicitly closed during database access, a large number of objects cannot be reclaimed, resulting in memory leaks.

  5. Improper use of variables

    The definition of a variable is larger than its scope and can cause memory leaks. On the other hand, if the object is not set to NULL in a timely manner, memory leaks are likely to occur

  6. Change the hash

    Once an object is stored in a HashSet, you cannot modify the fields in the object that contribute to the hash. Otherwise, the modified hash of the object will be different from the hash originally stored in the HashSet. In this case, even if the Contains method retrieves the object from the HashSet using the current reference to the object as an argument, it will return the result that the object is not found, This also causes the current object to be unable to be individually removed from the HashSet collection, causing a memory leak.

    This is why String is recommended as a Key for a HashMap, because strings are inherently immutable

  7. Cache leakage

    Another common source of memory leaks is caches, which are easy to forget once you put object references into the cache. For example, when a project is started, the cache is initialized. For example, a table in the database is loaded into memory. At this time, the data of this table is kept in memory during the project running

    For this problem, a WeakHashMap can be used to represent the cache. The feature of such a Map is that when there is no reference to the key other than the key itself, the Map will automatically discard the value.

  8. Listeners and callbacks


Afterword.

This is the first time that the author has completed a complete update of the series, and it is also the last post in the Bald Man series -JVM. I hope you can read more and share more. I’m L_Denny, a new generation of migrant workers. See you next time.