This is the 24th day of my participation in Gwen Challenge
Overview of garbage Collection concepts (PART 1)
1. The System. The understanding of gc ()
- It is called down by default
System.gc()
“, triggers the Full GC, which collects garbage from old and new generations and frees memory occupied by discarded objects. - but
System.gc()
Calls to the garbage collector are not guaranteed. - In general, garbage collection is automatic and no manual invocation is required.
2. Memory overflow or leakage
Memory Overflow (OOM)
In general, OOM is less likely to occur unless the amount of memory consumed by an application grows so fast that garbage collection can’t keep pace with memory consumption. The JavaDoc explanation for the OOM is that there is no free memory, and the garbage collector cannot provide more memory. There is usually a GC before an OOM, and an OOM occurs if the GC fails to resolve the problem. If the heap size exceeds the maximum heap size, throw OOM.
There are two reasons for not having free memory:
- The Heap memory setting of the Java virtual machine is insufficient
- A large number of large objects are created in the code and are long referenced and cannot be collected by the garbage collector.
Memory Leak
Strictly speaking, a memory leak is only when the object is not used by the program, but the GC cannot reclaim it. Or if something bad happens that causes the object to have a long lifetime or even an OOM, it can also be a memory leak, such as defining a variable that is only used in a method to a global variable, or a static variable.
A memory leak does not cause an application to crash immediately, but once it does occur, the application’s memory will be used up gradually and eventually OOM will appear.
Example of a singleton static object whose lifetime is the same as that of the JVM. If the object is referenced to another object, the referenced object will not be reclaimed even if it is not in use but is not dereferenced in time. For example, resources that need to be closed by calling close(), such as database connections, network connections, and IO connections, cannot be reclaimed if close() is not called manually.
3.Stop The World
STW, for short, refers to the application stalling during GC events, feeling that the entire application is suspended without any response.
Enumerating root nodes in the reachability analysis algorithm causes all Java execution threads to pause:
- Analysis must be performed under a consistent snapshot.
- Consistency refers to the fact that the entire execution system is frozen at a point in time during analysis.
- If the object is still changing during the analysis, the accuracy of the analysis result cannot be guaranteed.
Frequent STW occurrences will make users feel sluggish, so the occurrence of STW needs to be reduced.
STW doesn’t matter which garbage collector is used; all garbage collectors have STW events.