The vast sea of millions, thank you for this second you see here. I hope my series of interview questions can help you! ‘!

May you keep your love and go to the mountains and seas in the coming days!

Today’s Father’s Day, I hope I have the ability to support the family for my father in the future! Grow up together!

Three interview questions a day, achievement better self

Let’s continue talking about JVMS today!

1. Do you know when Full GC will trigger

  1. If the JVM is running out of space after a minor Gc or a large object or array is created, then the following error will be raised: Java. Lang. OutOfMemoryError: Java heap space.

    Solution: Try to make sure objects are collected during Minor GC, keep objects alive longer in the new generation, and don’t create objects and arrays that are too large.

  2. In our program, we call system. gc directly, and also start Full GC directly.

  3. If the CMS garbage collector is not configured, the CMS garbage collector will perform Full GC. If the CMS garbage collector is not configured, the CMS garbage collector will perform Full GC if the CMS garbage collector is not configured. If after the Full GC can’t recycle, then the JVM throws the following error message: Java. Lang. OutOfMemoryError: PermGen space.

    Solution: Increase the permanent generation space or switch to CMS GC.

  4. Promotion failed and Concurrent mode failure occur in CMS garbage collector

    For programs that use the CMS garbage collector for older GC, we need to look primarily at whether there are promotion failures and concurrent mode failures in the GC log, which can trigger Full GC:

    Promotion failed is caused when the Cenozoic Minor GC does not fit in the survivor area and objects can only be placed in the old age, which does not fit in the old age.

    Concurrent mode failure is a CMS error in which concurrent clear threads and worker threads work at the same time, cleaning up the space of the old generation is insufficient to hold objects promoted from the new generation to the old generation.

    Solution: Reduce the size of the young generation to avoid allocating too much space when putting in the old generation, adjust the rate of triggering the Full GC and increase the threshold for triggering the CMS GC appropriately

Not bad, not bad, not bad for you!

2. Have you seen four references in the JVM?

  • Strong references: The garbage collector does not collect strongly referenced objects.

    The most common in Java is strong references, where an object is assigned to a reference variable that is a strong reference. Even if we write something like User User = new User(), our new User object is a strong reference!

    When an object is referred to by a strongly referenced variable, it is reachable and cannot be collected by garbage collection. Even in the case of running OutOfMemory, the JVM would rather throw an OutOfMemory error than reclaim such an object.

  • Soft reference: An object that is not strongly referenced will be reclaimed for a second time before the system runs out of memory.

    A SoftReference is delivered using the SoftReference class. An object with only a SoftReference is not reclaimed when the system memory is sufficient, but is reclaimed when the system memory is insufficient. Soft references are usually used in memory-sensitive programs.

  • Weak references: Objects with weak references have a shorter lifetime. Objects that are not strongly referenced survive only until the next garbage collector. Whether there’s enough memory or not.

    WeakReference needs to be realized by WeakReference class, which has a shorter lifetime than soft reference. For objects with only weak references, as long as the garbage collection mechanism runs, regardless of whether the memory space of JVM is enough, it will always reclaim the memory occupied by the object.

  • Virtual reference: An object instance cannot be obtained by a virtual reference. The purpose of the virtual reference is to receive a notification when the object is collected by the garbage collector. The main purpose of a virtual reference is to track the status of an object being garbage collected.

Okay, one last thing for you:

3. Describe the main JVM parameters you know

  1. Heap Settings

    • -Xms: indicates the initial heap size
    • -Xmx: indicates the maximum heap size
    • -xx :NewSize=n: Sets the new generation size
    • ** -xx :NewRatio=n: ** Sets the ratio of the young generation to the old generation. For example, is 3, indicating that the ratio of Cenozoic era to old age is 1:3, and the Cenozoic era accounts for 1/4 of the sum of the whole Cenozoic old age
    • -xx :SurvivorRatio=n: ratio of Eden area to two Survivor areas in the Cenozoic era. Notice that there are two Survivor zones. For example, 3 means Eden: Survivor= 3:2, and a Survivor area accounts for 1/5 of the whole new generation
    • -xx :MaxPermSize=n: Set the persistent generation size
  2. Collector setup

    • -xx :+UseSerialGC: Sets the serial collector
    • -xx :+UseParallelGC: Sets the parallel collector
    • -xx :+UseParalledlOldGC: Sets the parallel age collector
    • -xx :+UseConcMarkSweepGC: Sets the concurrent collector
  3. Parallel collector setup

    • -xx :ParallelGCThreads=n: Sets the number of cpus used by the parallel collector for collection. Collect the number of threads in parallel.

    • -xx :MaxGCPauseMillis=n: Sets the maximum pause time for parallel collection

    • -xx :GCTimeRatio=n: Sets the percentage of garbage collection time in the running time of the program. Formula for 1 / (1 + n)

  4. Concurrent collector setup

    • -xx :+CMSIncrementalMode: set it to incremental mode. This mode applies to a single CPU.

    • -xx :ParallelGCThreads=n: Specifies the number of cpus used when the new generation collection mode of the concurrent collector is set to parallel collection. Collect the number of threads in parallel.

  5. Parameters for JVM tuning

    • ** -xms2G: ** Initialization push size is 2g;
    • ** -xmx2G: ** maximum heap memory is 2g;
    • ** -xx :NewRatio=4: ** Sets the memory ratio between young and old to 1:4;
    • ** -xx :SurvivorRatio=8: ** Sets the Eden and Survivor ratio of the new generation to 8:2;
    • ** — XX:+UseParNewGC: ** Specifies the use of ParNew + Serial Old garbage collector combination;
    • ** -xx :+UseParallelOldGC: ** Specifies to use the ParNew + ParNew Old garbage collector combination;
    • ** -xx :+UseConcMarkSweepGC: ** Specifies the use of CMS + Serial Old garbage collector combination;
    • ** -xx :+PrintGC: ** Enables gc information printing;
    • ** -xx :+PrintGCDetails: ** Prints GC details.

Nice guy! Today is over here, looking forward to your arrival tomorrow, I hope to continue to keep a surprise!

Note: If there are any mistakes and suggestions, please leave a message! If this article is helpful to you, I hope you will pay attention to it for three times. Thank you very much! You can also search the wechat prince Nezha public number private chat me, thank you guys!