What is GC in java?

Mondo Technology Updated on 2024-01-30

Q: What is GC?

Answer: In the J**a language,garbageCollection, GC) is a very important concept, and its main role is to use memory that is no longer used in the program

When using the C language for program development, developers must carefully manage the allocation and release of memory, if forgotten or mistakenly free memory, it will often lead to the program running unproperly or even crashing. In order to ease the developer's workload and increase the security and stability of the system, the J**a language provides a garbage user to automatically detect the scope of the object, which can automatically free up the storage space that is no longer used.

Specifically, the garbage collector is responsible for 3 tasks: allocating memory, ensuring that the memory of the referenced object is not mistakenly placed, and the memory space of the object that is no longer referenced.

On the one hand, the existence of garbage users frees developers from the complex work of freeing up memory, and improves the productivity of developersOn the other hand, the method of freeing up memory is shielded for developers, which can avoid the crash of the application caused by the developer's incorrect operation of memory, and ensure the stability of the program.

However, garbage also poses a problem, in order to achieve garbage, the garbage user must track the memory usage, release useless objects, and dispose of fragments in the heap after the memory is released, these operations will inevitably increase the burden on the JVM, thus reducing the execution efficiency of the program.

In the case of an object, if there are no variables to reference it, then the object will not be accessible by the program, so it can be considered spam and can be **. As long as there is more than one variable referencing the object, the object will not be spammed.

For the garbage user, it uses a directed graph to record and manage all objects in heap memory, and through this directed graph, you can identify which objects are "reachable" (with reference variables referencing it is "reachable"), which objects are "unreachable" (without referencing variables referencing it is unreachable), and all "unreachable" objects are trash, as shown below

After the above ** is executed to i2 i1, the reference relationship of memory is shown in the following figure:

At this time, if the garbage collector is doing garbage operation, the memory occupied by resource 2 is unreachable when traversing the directed graph above, and the garbage collector will think that this memory will no longer be used, so it will ** the memory space.

Garbage ** is carried out according to a certain algorithm,Here are a few of the commonly used garbage algorithms.

Reference counting collector

The main principle of reference counting as a simple but inefficient method is as follows: there is a reference counter for each object in the heap;When an object is referenced, the reference counter is incremented by 1;When a reference is left empty or out of scope, the reference count is reduced by 1, which is not used by the JVM because this method does not solve the cross-reference problem.

TracingCollector

The tracking algorithm uses the object reference graph maintained by the JVM to traverse the application graph of the object from the root node and mark the objects traversed. When the traversal is over, the unmarked object is the object that is currently unused and can be **.

CompactingCollector

The main idea of the compression algorithm is as follows: move the active objects in the heap to one end of the heap, so that a large free area will be left at the other end of the heap, which is equivalent to processing the fragments in the heap. While this approach greatly simplifies the effort of removing heap fragmentation, it comes with a performance penalty with each processing.

Replication algorithm (copingcollector).

The main idea of the replication algorithm is as follows: divide the heap into two regions of the same size, at any time, only one of the regions is used until this area is consumed, at this time the garbage user will interrupt the execution of the program, and copy all the active objects to another area by traversing, and they are arranged next to each other in the process of replication, so that the memory fragmentation can be eliminated. When the copying process is over, the program will continue to run until the area is used up, and then continue to garbage using the method above**.

The advantage of this algorithm is that the arrangement of objects is also arranged at the same time as the garbage **, thus eliminating memory fragmentation. But it also comes at a high price: twice the size of memory space is required for a heap of the specified size;At the same time, because the currently executed program is interrupted in the process of memory adjustment, the execution efficiency of the program is reduced.

GenerationalCollector

The main disadvantage of the replication algorithm is that every time the algorithm is executed, all active objects are copied, which is very inefficient. Since the program has the characteristic that "most of the objects created by the program have a short lifespan, and only some objects have a long lifespan", the algorithm can be optimized according to this feature. The main idea of the generational algorithm is as follows: divide the heap into two or more sub-heaps, and each sub-heap is considered a generation. The algorithm prioritizes the collection of "young" objects as it runs, and if an object is still "alive" after multiple collections, then the object can be transferred to a higher heap to reduce the number of scans on it.

Frequently Asked Written Questions:

The following are available**:

When the float object is created on the second line, when can it be garbage**?

a.4 lines later.

b.After 5 lines.

c.6 lines later.

d.After 7 lines.

Answer: c. After line 6, there are no more objects referencing float objects, so they can be garbage**.

2.Of the following statements about garbage**, the correct one is ().

a.As soon as an object becomes garbage, it is immediately ** off.

b.After the object space is dropped, the finalize method of the object is executed.

c.The finalize method and the destructor of c are the same thing entirely.

d.An object becomes garbage because there are no more references pointing to it, but threads don't.

Answer: d. The object that becomes garbage will only be cleaned up the next time the garbage user runs, not right away, so option A is wrong. The finalize method is called before the objectspace is **, so option b is wrong. In the C language, after the destructor is called, the object will be destroyed, and the J**A language calls the finalize method, but the garbage will not necessarily be **, so the finalize method is different from the destructor of C, so the option C is also incorrect. For d, an object can be garbage when it is no longer referenced, but threads can run independently even if they are not referenced, so unlike objects. So the correct answer is d.

3. Is it possible to actively notify the JVM to be garbage**?

Answer: Due to the existence of the garbage user, the J**a language itself does not provide the developer with a way to explicitly release the allocated memory, that is, the developer cannot call the garbage user in real time to garbage an object or all objects. However, developers can do this by calling systemgc() method to "notify" the garbage user to run, and of course, the jvm does not guarantee that the garbage machine will run immediately. Due to systemThe execution of the gc() method will stop all responses and check whether there are objects in memory, which will pose a great threat to the normal operation and performance of the program, so it is not recommended to use this method frequently when actually programming.

Related Pages