Java's memory leak and garbage collection mechanism

Source: Internet
Author: User

Does Java produce memory leaks? First of all, the answer is yes. Java has a garbage collector, but leaks still exist.

Java memory leaks are not the same as the C + + memory leak concept: the memory leak in C + + means that malloc has some resources, finally no free, memory is not recycled, this is the real memory black hole, and the Java leak means that the existence of Java objects is meaningless to the program, But objects persist throughout the program cycle, which can lead to a memory leak in Java.

To summarize the reasons, there are probably the following aspects:

(1) An object in Java is improperly held by a class that has a long life cycle, which is the main reason for a Java memory leak, where the object is not actively purged in a task, and its host increases the number of references to the object as it works, and it eats more memory, causing a memory leak When a programmer inadvertently forgets to dismiss a reference that an object should not have, the memory leak is still unavoidable.
(2) when the runtime exception, there is no use of finally{} to release certain resources;
(3) The expansion of the range of variables, such as a method of the local variable is defined as a class of variables, so that the life of the variable is longer, it is possible to cause a leak (this and the 1th is a bit like).


Java's memory leak and garbage collection mechanism

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.