Valid Java (6)-eliminates expired object references

Source: Internet
Author: User

I. Introduction

Many people may be thinking about the problem: Is there a garbage collection mechanism in Java, so there is still Memory leakage? The answer is yes. The so-called garbage collection GC will automatically manage the memory collection, without the need for programmers to manually release the memory every time, however, if a large number of temporary objects are not referenced when they are not used, a large amount of memory will be swallowed up and memory overflow will soon occur.


Ii. Java garbage collection mechanism

Objects in Java are allocated in the heap. There are two ways to create an object: new or reflection. The collection of objects is through the garbage collector. The JVM Garbage Collector simplifies the programmer's work, but increases the JVM's work. This is one of the reasons why Java program runs slowly, in order to release objects correctly, GC must monitor the running status of each object, including application, reference, reference, and assignment of objects. GC must monitor all objects, the state of the Monitored object is to release the object more accurately and timely. The fundamental principle of releasing the object is that the object is no longer referenced.


Iii. Memory leakage in Java

Memory leakage objects have the following two features:

① These objects are reachable, that is, the paths in the directed graph can be connected to them.

② These objects are useless, that is, they will not be used by the program in the future.

public class Stack {private final static int MAX_ATTRIBUTE = 10;private Object[] arr;private int index = 0;;public void push(Object obj) {if (index > 9)throw new IllegalArgumentException();arr[index] = obj;index++;}public Stack() {arr = new Object[MAX_ATTRIBUTE];}public Object pop() {if (index < 0)throw new IllegalArgumentException();return arr[--index];}}
What happens to memory leakage in this program? If a stack grows first and then shrinks, the objects popped out from the stack will not be recycled as garbage. Even if the applications using the stack no longer reference these objects, they will not be recycled, because the stack maintains the expired reference to these objects.

public Object pop() {      if (size == 0)          throw new EmptyStackException();      Object result = elements[--size];      elements[size] = null; //Eliminate obsolete reference         return result;  }  
Solution: As long as an element is popped up With a stack, it will be referenced as null and GC will be recycled.


Iv. Common Memory leakage

① Static collection classes such as HashMap and Vector are most likely to cause memory leakage because the lifecycle of these static variables is consistent with that of the application.

VectorVector = new Vector(10); for (int I = 1; I <100; I ++) {Object obj = new Object (); vector. add (obj); obj = null ;}

Put the object into the Vector. If only the reference itself is released, but the Vector still references this object, this object cannot be recycled by GC. If you want to recycle it, the simplest way to do this is to set the Vector object to null.


② In Java programs, we often have to deal with listeners. We usually call methods such as addXXXListener () to add listeners, but often forget to delete these listeners, which increases the chance of Memory leakage.


③ When using the connection pool, in addition to explicitly closing the connection, you must also explicitly close the Resultset and Statement objects. Otherwise, a large number of these objects cannot be released, causing memory leakage.



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.