"Java plane question" Is there a memory leak in Java, please describe it briefly.

Source: Internet
Author: User

The so-called memory leak refers to an object or variable that is not being used by the program has been occupied in memory. There is a garbage collection mechanism in Java that ensures that an object is no longer referenced when the object is programmed to orphan, and the object is automatically erased from memory by the garbage collector. Because Java uses a graph-like approach to garbage collection management, you can eliminate the problem of reference loops, such as having two objects and referencing each other as long as they and the root process are unreachable, and GC can reclaim them.

Package Com.huawei.interview;

Import java.io.IOException;

public class Garbagetest {

/** * @paramargs *@throwsIOException*/ Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method Stub    Try{gctest (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Has exited gctest!");    System.in.read ();           System.in.read (); System.out.println ("Out Begin gc!");  for(inti=0;i<100;i++) {System.GC ();           System.in.read ();       System.in.read (); }}Private Static voidGctest ()throwsIOException {System.in.read ();           System.in.read (); Person P1=NewPerson ();    System.in.read ();           System.in.read (); Person P2=NewPerson ();    P1.setmate (p2);    P2.setmate (p1); System.out.println ("Before Exit gctest!");    System.in.read ();           System.in.read ();    System.GC (); System.out.println ("Exit gctest!");}Private Static classperson{byte[] data =New byte[20000000]; Person Mate=NULL;  Public voidsetmate (person other) {mate=Other ; }}}

Memory leaks in Java: Long life-cycle objects that hold references to short life-cycle objects are likely to have a memory leak, even though short life-cycle objects are no longer needed, but because long-life-cycle objects hold their references and cannot be recycled, this is where memory leaks occur in Java, in layman's words, Is that the programmer might have created an object that would never be used in the future, and this object has been referenced, that is, the object is useless but cannot be reclaimed by the garbage collector, which is a possible memory leak in Java, for example, the cache system, we loaded an object in the cache ( For example, in a global map object), and then no longer using it, the object has been cached for reference, but is no longer used.
Check for memory leaks in Java, be sure to let the program complete the various branches of the program to the end, and then see if an object has been used, if not, you can determine that the object is a memory leak.

If the method of an instance object of an external class returns an instance object of an inner class, the inner class object is referenced for a long time, even if the Outer class instance object is no longer being used, but because the inner class persists the external class's instance object, the Outer class object will not be garbage collected, which will also cause a memory leak.

public class Stack {
Private object[] Elements=new object[10];
private int size = 0;
public void push (Object e) {
Ensurecapacity ();
elements[size++] = e;
}
Public Object pop () {
if (size = = 0)
throw new Emptystackexception ();
return elements[–size];
}
private void Ensurecapacity () {
if (elements.length = = size) {
object[] oldelements = elements;
elements = new object[2 * Elements.length+1];
System.arraycopy (oldelements,0, elements, 0, size);
}
}
}

Another case of memory leaks: When an object is stored in the HashSet collection, it is not possible to modify the fields in the object that participate in the hash value, otherwise the hash value of the modified object is different from the one originally stored in the HashSet collection, in which case Even if the Contains method uses the object's current reference as a parameter to retrieve the object in the HashSet collection, the result of the object cannot be found, which also causes the current object to be deleted from the HashSet collection, causing a memory leak

"Java plane question" Is there a memory leak in Java, please describe it briefly.

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.