Summary of Java garbage collection

Source: Internet
Author: User

memory is a scarce resource, even if the memory of a piece of money! If used improperly in programming, then large memory will also consume light. I. Understanding Java's automatic garbage collectiongarbage Collection is a major feature of the Java language and facilitates programming, at the expense of performance. And rubbish here is only useless objects. C + + requires programmers to write their own destructors to free memory, trouble, and possibly forget to cause memory leaks. The Java language's allocation management of memory is determined by the internal mechanism of the JVM. Programmers can not care about their handling. second, the principle and significance of garbage collectionThere's something called a garbage collector in a Java virtual machine that actually doesn't exist or is already integrated into the JVM, but it doesn't matter, and we can still call it a garbage collector. The purpose of the garbage collector is to find and reclaim (clean) useless objects. To allow the JVM to use memory more efficiently. the runtime of the garbage collector is indeterminate and is determined by the JVM and executed intermittently at run time. Although it is possible to force the garbage collection through System.GC (), the command is released and there is no guarantee that the JVM will respond to execution immediately, but experience has shown that your request will be executed shortly after the command is issued. The JVM usually feels a memory crunch to perform garbage collection operations. too often, garbage collection can cause performance degradation, which is too sparse to cause memory shortages. This JVM will control it to the best, without the programmer worrying about it. But some programs eat a lot of memory in the short term, and these scary objects are quickly used, and it may be necessary to force a garbage back command, which is necessary to have more available physical memory. It is learned from the above that useless objects are rubbish. accurately, when no thread accesses an object, the object is eligible for garbage collection. for string, there is a string pool, which is outside the scope of this article, garbage collection in a string pool, algorithms and garbage collection discussed here are completely different. But it has to be said that the random concatenation of strings, often lead to a sharp decline in performance, especially in the huge loop, stitching strings is to let the program of chronic suicide. This is also a problem that many Java programmers are prone to make. since the string is a pool, it is intended to be buffered, so that the frequency of garbage collection may be much lower than the JVM object garbage collector in order to have a higher hit rate. the only thing the garbage collector can do is to make the available memory as efficient as possible, so that available memory is managed efficiently. Programmers can influence the execution of garbage collection, but they cannot control it. iii. programmatic impact of garbage collectionAlthough programmers cannot control the garbage collection mechanism of the JVM. However, it can be influenced by programmatic means to make the object conform to the garbage collection conditions. There are a few of them, respectively:1. Assign a value of NULL to a useless object.2. Re-assign a value to the reference variable. For example:Person p = new Person ("AAA"), p = new Person ("BBB");in this way, the object of new person ("AAA") is garbage--it is eligible for garbage collection. 3. Let the object of mutual contact be called "Island" ObjectPerson p1 = new Person ("AAA"); person P2 = new Person ("BBB"); Person P3 = new Person ("CCC"); P1=P2; P2=P3; P3=P1; P1=null; P2=null; P3=null; before P1, p2, and P3 are null, they are a kind of triangular love relationship. Null is placed separately, and the relationship between triangles is still there, but three variables are not used. Three person objects make up an island and end up dead on the heap-garbage collected. 4. Mandatory garbage collection System.GC ()In fact, the force here is the programmer's will, recommendations, when execution is the JVM garbage collector to the final decision. calling garbage collection does not necessarily guarantee that unused objects will be removed from memory. The only guarantee is that when there is very little in your case, the garbage collector will run once before the program throws Outofmemaryexception. four, very mysterious Finalize () methodThe Finalize () method is really mysterious because you don't understand its principle. principle: 1, Finalize () method is the method in object. 2. The Finalize () method is called by the garbage collector once before the object is garbage collected, which is a mechanism of the Java language. 3. The Finalize () method is called only once by the garbage collector on any object. traps: 1, the garbage collector cannot guarantee that garbage objects can be recycled, so the Finalize () method is not guaranteed to run. It is recommended that you do not rewrite the Finalize () method, even if overridden, and do not write critical code in the Finalize () method. 2, the Finalize () method can pass themselves to individual objects, so it is not garbage, to avoid being recycled. But the next time this object is eligible for garbage collection, the Finalize () method is not called the second time, but is cleaned up directly. Summary:Understanding garbage collection is premised on understanding the Java Runtime's memory stack model. The purpose of understanding Java garbage collection is to have a knowledge of Java memory management and to use memory more efficiently when programming. It's foolish to write a lot of code manually for garbage collection, which is not recommended. Can be affected in a simple way. This article discusses a garbage collection that excludes a string object. The garbage collection of string is very much related to the string pool and is not yet studied. However, it has been mentioned that there are problems that are prone to use in string.

Summary of Java garbage collection

Related Article

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.