What is the "Java Plane question"? Why do you have a GC?

Source: Internet
Author: User

GC is the meaning of garbage collection (Gabage Collection), memory processing is where programmers are prone to problems, forgetting or wrong memory recycling can cause program or system instability or even crashes,
The GC functionality provided by Java can automatically monitor whether an object exceeds the scope to achieve the purpose of automatically reclaiming memory, and the Java language does not provide a way to release the displayed operation of the allocated memory.

GC is a garbage collector. Java Programmers don't have to worry about memory management because the garbage collector is automatically managed. To request garbage collection, you can call one of the following methods:

System.GC ()
Runtime.getruntime (). GC ()

Java is developed by C + +.

It rejects some of the tedious and error-prone things in C + +. One of them is this GC.

To write a C + + program, the programmer defines a variable that opens up a corresponding space in memory to store the value. The memory size is also limited, so when the program no longer needs to use a variable, it is necessary to release the memory space resources, so that other variables to use it. In C + +, it is up to the programmer to release the memory space of the unused variables. This means that when a programmer thinks a variable is useless, it should write a code that frees up the memory it occupies. This minimizes memory leaks and resource waste.

But this is obviously very cumbersome. Programs are large, and when variables are many, programmers forget to free up memory or release memory when they are not released. and freeing up memory this kind of thing, from the development point of view, should not be the programmer should pay attention to. What programmers should do is to implement the required program functions, rather than devote a lot of effort to the allocation of memory to release.

With a GC, Java does not require programmers to manually free up memory space. When the Java virtual machine discovers that the memory resource is tense, it automatically cleans up the memory space occupied by the useless variable. Of course, if needed, 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 heap memory, that is, the complete GC, it is necessary to stop all activities (Stop the World Collection), your application can withstand this? and its display call to System.GC () is simply a recommendation to the virtual machine, not necessarily executed, because System.GC () executes in a low-priority thread.

What is the "Java Plane question"? Why do you have a GC?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.