Java Beginner-Garbage collector

Source: Internet
Author: User

Ava Beginner Practice Tutorial 14-garbage collector
People who have used C + + programming know that they always have to keep track of the objects they create, and that they need to be shown to remove unwanted objects. This way is too troublesome, error prone. Write so much code, can remember, if the useful to delete how to do, if there is no use to forget how to do. These problems are very serious. The garbage collector used in the Java language manages memory in this way, and is convenient and safe. Garbage collector, you can automatically determine which object is no longer being exploited and it can be automatically deleted. This is also a big advantage of the Java language.
We want to show the deletion of a reference to an object is also very simple, the reference to the variable assignment is null not on the line. For the garbage collector, when the programmer creates the object, the garbage collector begins to monitor the address, size, and usage of the object. Typically, the garbage collector records and manages all objects in the heap (heap) in a graph-by-map way. This way you determine which objects are "available" and which objects are "unavailable." When the garbage collector determines that some objects are "not available," The garbage collector reclaims the memory space.

However, the garbage collector in the system in a lower priority to perform in the idle cycle, the popular point is that it is low level, others do not run when the turn to it, so the speed of the garbage collector is relatively slow. There are times when we use System.GC (). Manual recycling. This will improve performance.

Another thing worth mentioning about the garbage collector is the Finalize () method, in which each object has a finalize () method, which is inherited from the object class. It is used to reclaim system resources outside of memory, like file processors and network connectors. The invocation order of the method is independent of the order in which the object used to invoke the method is created. In other words, the order of the method and the actual invocation order of the method are irrelevant when writing the program. This is only the characteristic of the Finalize () method. Also, each object can invoke only the Finalize () method once. If an exception (exception) is generated when the Finalize () method executes, the object can still be collected by the garbage collector. That is certain, can not say to use Finalize (). The garbage collector doesn't do anything. Finalize () The workload is very big OH

Summarize:

Java uses a garbage collector's memory management method, not that it is completely good. Sometimes it affects its performance, and we still have to collect it manually. But it would be too cumbersome and not very safe to collect it as a complete manual, like C + +.

According to how the garbage collector works, we can make the garbage collector run more efficiently by some techniques and methods.

1. The most basic advice is to release the references to unwanted objects as soon as possible.

Most programmers use temporary variables when they leave the reference variable automatically set to null after exiting the active field (scope).

2. Use the Finalize function as little as possible. A finalize function is the opportunity that Java provides to programmers to release objects or resources. However, it increases the workload of the garbage collector and therefore minimizes the use of a Finalize method to reclaim resources.

3. When the program has a certain waiting time, the programmer can manually execute System.GC (), notify the garbage collector to run, but the Java language specification does not guarantee that the garbage collector will execute.

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.