How to prevent memory leaks in Java

Source: Internet
Author: User
Tags mail exchange

A few days ago, the emergency publishing, found a memory leak problem, the program every logoff will have a large number of objects are not released, in
When the logout login succeeds, a similar object is regenerated. In this way, in the process of doing interface automation testing, the system
Frequent cancellation, landing, and then cancellation. This so many times, will inevitably lead to Java this process memory overflow outofmemory.

Get the problem, use Jprofile to run the program, find the specific leakage of the object, and then carry out a detailed analysis.
A serious leak was found in two places:
1 An object is created, the process of initialization creates a thread that specializes in communication with the computer's serial port.
The code is as follows:
package example;

/**
* @author zLan1028
*
* TODO to change the template of this generated type comment go
* Window-preferences-java-code Style-code Templates
*/
public class Memoerytest
{

public static void Main (string[] args)
{
Memoerytest test = new Memoerytest ();
Test.init ();

Test = null;
Do other Something
}

public void Init ()
{
Thread.Start ();
}

Private thread thread = new Basicthread ();
Boolean exit = FALSE;
Class Basicthread extends Thread ()
{
List lstdata = new LinkedList ();
public void Run ()
{
while (!exit)
{
Synchronized (Lstdata)
{
if (Lstdata.isempty ())
{
Lstdata.wait ();
}
Writedatatoserialport ((String) Lstdata.removefirst ());
}
}
}

private void Writedatatoserialport (String str)
{
Write data to SerialPort
}
};
}
See if the object will automatically recycle the garbage collector after the test object has been set to null.
At first glance, it should be, this object already has no other references. It's not, because an inner class is created in this object.
In Java, an inner class automatically gets an object reference to an external category, and when the test object is released, there is no thread object created inside it,
So this thread object always exists before the program exits, so it keeps a reference to the external test object, so that each create a memoerytest
Object, there will be a thread object leak and a Memoerytest object leak. It's very dangerous.
This problem can be avoided if the object is initialized only, and the thread resource that is created is not reclaimed when the program logs off.
The code is as follows:
Add a public void close () method in the Memoerytest object to voluntarily call the Close method to release the resource each time the Memoerytest object is released.

public void Close ()
{
try {
Exit = true;
Thread.Interrupt ();
Thread.Join ();
catch (Exception e)
{
E.printstacktrace (System.out);
}
}

A little accumulation, if there is a mistake, but also please heroes guide. Welcome Mail exchange: abin2012@21cn.com.

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.