GC means garbage collection (Gabage Collection). Memory processing is where programmers are prone to problems. Forgotten or wrong memory recycling can lead to instability or even crash of the program or system.
The GC function provided by Java can automatically detect whether the object exceeds the scope to achieve the purpose of automatically reclaiming memory. The Java language does not provide release. Display operation method for allocated memory. (Recommended learning: java course )
GC is a garbage collector. Java programmers don't have to worry about memory management because the garbage collector takes care of it automatically. To request garbage collection, you can call one of the following methods:
System.gc() Runtime.getRuntime().gc()
Java is developed from C.
It abandons some of the cumbersome and error-prone things in C. One of them is this GC.
When writing a C/C program, the programmer defines a variable, which means opening up a corresponding space in the memory to store the value. No matter how large the memory is, it is limited, so when the program no longer needs to use a certain variable, it needs to release this memory space resource so that other variables can use it.
In C/C, the issue of releasing useless variable memory space must be solved by the programmer himself. That is to say, when the programmer thinks that the variable is useless, he should write a code to release the memory it occupies. In this way, memory leaks and resource waste can be avoided to the greatest extent.
But this is obviously very cumbersome. When the program is relatively large and there are many variables, programmers often forget to release the memory or release the memory when they should not be released.
And releasing memory, from a development perspective, should not be something that programmers should be concerned about. What programmers have to do should be to implement the required program functions instead of spending a lot of energy on allocating and releasing memory.
With GC in Java, programmers do not need to manually release memory space. When the Java virtual machine finds that memory resources are tight, it will automatically clean up the memory space occupied by useless variables. Of course, if necessary, programmers can explicitly use System.gc() in a Java program to force an immediate memory cleanup.
Because the explicit declaration is to do a full scan of the heap memory, that is, Full GC, all activities need to be stopped (Stop The World Collection). Can your application withstand this?
It shows that calling System.gc() is just a suggestion to the virtual machine and may not be executed because System.gc() is executed in a thread with a very low priority.
The above is the detailed content of java what is gc. For more information, please follow other related articles on the PHP Chinese website!