Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

Because I have a preliminary understanding of the garbage collection mechanism in Java in a video of Teacher Gao Qi from B Station. In order to consolidate what you have learned, output it again to test your absorption.

For a program to run, space is created in memory and then freed. In C++, this release action is done manually by the programmer 👨🏻💻 himself. In Java and Python, recycling is done automatically.

To recycle, you first need to know who is taking up most of the space. Most of it is taken up by objects in the heap. When are objects in the heap created? Object name = new Object(); Such code is used to allocate memory space.

Garbage Collection consists of two parts:

  1. Garbage found: the object to be collected is not referenced by any variable.
  2. Garbage collection: assign all reference variables to NULL.

There are two algorithms for finding garbage:

  1. Reference counting method: by counting to confirm that the number of times the object is referenced is 0;
  2. Reference unreachable method: reference graph concept, there will be a reference relationship “point – point” this relationship, then a single node is “unreachable”.

Note:

  1. We don’t have to recycle;
  2. System.gc()“, can only make suggestions, although it will usually be implemented but don’t mess with it, because implementation once is activatedfull GC, relatively high cost, cost performance;
  3. finalizeMethods are methods used to free objects or resources, but use them sparingly.

In a telling story analogy, it’s easy to remember the generational mechanism in Java. There are three status zones, which are analogous to those in a hotel with a lobby:

  • The young generation,Eden + Survivor1, Survivor2(These three words represent three Spaces)

— The fast food area. As soon as you come in, you will be in Eden District. You will buy your food and leave with your lunch box.

If there is a Survivor1, Survivor2 table next to the Survivor1, Survivor2 table, repeat these two S words more than 15 times, then sorry, you are no longer young;

  • Old generation,Tenured

— Private room, sit and eat slowly, then go to sit and eat slowly, more than 15 times, there is no way, at this time, if the waiter wants to clean, it will be upgraded, it will be cleaned by Major GC. The Full GC is like the cleaning manager, starting with a command that can clean both the old and young generations.

  • The permanent generation

Permanent storage, the program is in, until closed, mainly put static variables, class information (method area).