• Approaching autumn recruitment, preparing for summer internship, I wish you every day progress hundred million little!Day08
  • This post is a summary of jVM-related interview questions, which will be updated daily


1. Exception architecture in Java

Java deprecates that all exceptions come from the top-level parent Throwable.

There are two subclasses of Throwable, Exception and Error, to indicate that something abnormal has occurred in the program. The difference is:

  • Error is program Error, usually for a virtual machine related errors, such as system collapse, out of memory, such as stack overflow, the compiler will not detect this kind of mistake, JAVA applications do not deal with this kind of mistake is captured, once this kind of Error occurs, usually the application will be terminated, only on the application itself cannot recover;

  • An Exception is a program Exception that can be caught and handled within an application. It is a design or implementation problem. That is, it represents a situation that would never occur if the program worked properly.

    • ExceptionIt is divided into two parts: runtime exceptions (RunTimeException) and check for exceptions (CheckedException).
    • RunTimeExceptionOccurs when a program is running and causes the current program thread to fail (this does not affect other threads, such as the null pointer exception ~).
    • CheckedExceptionThis occurs when a program is being compiled, resulting in a compile failure (also known as a compile-time exception).

2. Does garbage collection occur in persistent generations in the JVM?

Garbage collection occurs, and if the generation is permanently Full or the threshold is exceeded, a Full GC is triggered.


3, briefly describe the implementation process of generational garbage collection algorithm?

  • Generation collection algorithm: This algorithm divides the Java heap into the new generation and the old generation. By default, the new generation takes up 1/3 of the total space, and the old generation takes up 2/3 of the total space. The most appropriate collection algorithm is adopted according to the characteristics of different ages.

    • In the new generation, a large number of objects are found dead and only a few survive in garbage collection, so the replication algorithm is selected, and only a small amount of the replication cost of the surviving objects can be collected.

      • There are three zones in the Cenozoic: Eden, To Survive, and From Survive, and their default ratio is8:1:1.
    • In the old days, because the object had a high survival rate and there was no extra space to allocate it, it had to be recycled using mark-clean or mark-tidy algorithms.

The following is a step-by-step introduction to the process of generational collection algorithm:

  • Objects that have been used for a long time are placed in the old age (recycling once for a long time takes a long time), and objects that can be discarded when used up are placed in the new generation (recycling is frequent and relatively fast) :

  • The newly created objects were placed in the Eden of the new generation:

  • When Eden runs out of memory, a garbage collection is made, called a Minor GC (Young GC) :

The Minor GC copies Eden and surviving FROM objects that still need TO survive into surviving FROM, increases their lifetime by 1, and then swaps FROM and TO.

  • Eden does not require a living object to clear it:

  • Swap FROM and TO:

  • Similarly, continue to add objects to Eden, if full, proceed a second timeMinor GC:

Same process, still need to live object life +1 :(in the picture below, the object with life of 1 in the FROM is newly copied FROM the garden of Eden, not the object with life of 1 in the original survival zone FROM, here is only a static picture is not easy to display, can only be described in words)

The object is created again, and if The Eden of The new generation is full again, The Minor GC (which triggers Stop The World, stopping other user threads and letting only The garbage collection thread work) is triggered again, which not only collects The garbage from The Eden, but also collects The garbage from The survivable zone and copies The active object TO The survivable zone TO. Recycling swaps the two surviving zones and increases the life of objects in the surviving zone by 1!

  • If the object in the surviving zoneThe service life exceeds a threshold. Procedure(maximum15.4 bit), will bePut into old ageIn:

  • If memory is full in the new generation and the old generation, it will trigger firstMinor GcAnd then triggerFull GC, the scanningNew generation and old ageAll objects that are no longer used and recycled:

Process summary of generational collection algorithm:

  • Newly created objects are first assigned to the Eden region.
  • Cenozoic space is insufficient when triggeredMinor GC, Eden andFROMObjects that need to survive in the survival zone will be copiedTOIn the survival zone, the life of the surviving object+ 1And exchangeFROMTO.
  • Young GCWill lead toStop The World: Suspends another user’s thread and waits for the garbage collection to complete before resuming execution.
  • The service life of an object exceeds the threshold15Will be promoted to the old age.
  • If the memory of both the new generation and the old generation is full, it will trigger firstMinor GCAnd then triggerFull GC, the scanningNew generation and old ageAll objects that are no longer used and recycled.

4. In the New generation, what is the default ratio between Eden Park and FROM and To surviving zone?

In HotSpot VIRTUAL machine, the default ratio of Eden and Survivor zones is 8:1:1, namely -xx :SurvivorRatio=8, where Survivor is divided into From Survivor and ToSurvivor, so Eden accounts for 80% of the new generation space.


5. What is HotSpot GC classification?

  • Young GC/Minor GC: Collect only new generation GC.
  • Full GC/Major GC: Collects the entire GC heap, including all parts of the Cenozoic, old, and permanent generations (if any).

6. What triggers HotSpot GC?

Young GC: Young GC is triggered when there is not enough space in the Eden sector of the new generation to allocate.

Full GC:

  • When ready to trigger onceYoung GCWhen, if found statistics are said beforeYoung GCIf the average promotion size is larger than the remaining space in the current old age, it will not triggerYoung GCIt turns into a triggerFull GC. (Usually)
  • If there is a permanent generation, it also fires when the permanent generation needs to allocate space but has run out of spaceFull GC.
  • System.gc()Default is also triggerFull GC.

7. Under what circumstances will the younger generation be promoted to the older generation?

  • Newly created objects are put into the Eden, which is triggered when there are too many objects in the Eden to have enough memory for new objectsYoung GC
  • Every timeYoung GCWill beEden and the surviving zone FROMYou still need live objectsFirst,Copied to theSurvival area TOAnd let itAdd 1 lifeAgain,Swap FROM and TO.
  • If the object in the surviving zoneThe service life exceeds a threshold. Procedure(maximum15.4 bit), will bePut into old ageIn the.
  • If the memory of both the new generation and the old generation is full, it will trigger firstYoung GCAnd then triggerFull GC, the scanningNew generation and old ageAll objects that are no longer used and recycled.

Do I need to scan older objects when Young GC occurs?

Don’t!

  • In the separation and collection concentration, the scale of the new generation is generally much smaller than that of the old age, and the collection of the new generation is much more frequent than that of the old age. If the recycling of the new generation also has to scan the old age, then the efficiency of the Young GC may decrease a lot, and it is obviously impossible to scan the old age.

How does the JVM determine a class? (How does the JVM determine that classes are identical?)

In Java, a class is identified by its full name (package name + class name), but in the JVM, a class is uniquely identified by its full name + classloader. Classes loaded by different class loaders are placed in different namespaces. This is called classloader isolation.

See article: How can a class be uniquely identified in a JVM


10. New Object and wrapper types. How many bytes is the minimum for an Object?

  • Least one object16Bytes.

Reference article: Selected Java Virtual Machine interview questions

Summary of the interview question is also quite time-consuming, the article will be updated from time to time, sometimes more than a day to update a few, if you help to review and consolidate the knowledge point, please also support three, the follow-up will be a hundred million points of update!


In order to help more young white from zero to advanced Java engineers, from CSDN official side to get a set of “Java Engineer learning growth knowledge Map”, size870mm x 560mm, can be folded into a book size, interested friends can have a look, of course, anyway, the blogger’s articles are always free ~