Home  >  Article  >  Java  >  What is gc in java

What is gc in java

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-11-11 14:40:524278browse

What is gc in java

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 a display operation method to release allocated memory.

GC is the 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, 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. Moreover, releasing memory should not be something that programmers should pay attention to from a development perspective. 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.

php Chinese website, a large number of free

Java introductory tutorials, welcome to learn online!

The above is the detailed content of What is gc in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn