java-07-garbage collection

Traditional programming languages ​​such as C/C++ need to display garbage collection, which shows that garbage collection has two disadvantages.
1. The recycled content is not recycled, which leads to memory leaks and reduces system performance.
2. The memory that should not be recycled (core library memory) is recycled. ), causing the system to crash

The memory allocation and recycling of Java programs are automatically performed by the JRE in the background.
JRE will provide a background thread for detection and control. Generally, garbage collection is automatically performed when the CPU is idle or the memory is insufficient.
Java's heap memory is a runtime data area that is used to store instances (objects) of classes. All JVMs have a heap memory managed by a garbage collector.

The goal of garbage collection is to reclaim the memory space of useless objects. These memory controls are the memory space in the JVM heap memory. Garbage collection can only reclaim memory resources. For other physical resources, such as database connections, disk I/O, etc. Powerless.

In order to allow the garbage collection mechanism to reclaim the objects that are no longer used more quickly, the reference variable of the object can be set to null, which implies that the garbage collection mechanism can reclaim the object.

Because different JVMs use different garbage collection mechanisms and different garbage collection algorithms, it may happen regularly, it may happen when the CPU is idle, or it may be the same as the original garbage collection, and wait until memory consumption occurs. When the limit occurs, although the programmer can call the Runtime object gc() or system.gc() and other methods to suggest that the system perform garbage collection, the only time to call the three is recommended, but it still cannot accurately control the execution of the garbage collection mechanism.

Some stop the running of the application when the garbage collection starts, some allow the thread of the application to run when the garbage collection is running, and some allow the garbage collection to run in multiple threads at the same time.

In fact, in addition to freeing useless objects, garbage collection can also clear memory record fragments.

Guess you like

Origin blog.csdn.net/puyu2017/article/details/106336076