Java memory leaks

Source: Internet
Author: User

A Java memory leak is divided into two main areas:

(1) The space requested in the heap has not been released.

(2) The object is no longer in use, but still exists in memory

The following concentration conditions can cause a memory leak

(1) Static collection using HashMap and vectors, the life cycle of the static container is consistent with the program, then the container will not be released before the end of the program, resulting in a memory leak;

Like what:

Vector v=new  vector ()for (int i=0;i<10;i++) {object o=new object (); V.add (o);}


o objects are no longer needed, but cannot be recycled

(2) Various connection problems: Database connection and IO connection, need to call the Close method to release the connection, such as connection, statement, resultset

(3) Use of the listener

(4) Irrational scope of variables

1) The scope of a variable definition is greater than the scope of its use:

The scope of use of MSGD should be in the aspect

Class server{private string msg;  void//// input db }}       

2) The object is not set to NULL in time

or set MSG to NULL after use

(5) Use of single-case mode

class bigclass{.....  }classsingleton{Private  bigclass BC, ......... }//BC objects cannot be recycled

The garbage collection algorithm has

(1) Reference counting algorithm

(2) Tracking recovery algorithm, reference diagram

(3) Compression recovery algorithm

(4) Replication recovery algorithm

(5) Recycling algorithm by generation

Java memory leaks

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.