Java Memory leakage and java Leakage

Source: Internet
Author: User

Java Memory leakage and java Leakage

 

Java Memory leakage is mainly divided into two aspects:

(1) The space applied for in the heap is not released

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

Memory leakage may occur in the following cases:

(1) When a static set uses hashmap and vector, the lifecycle of the static container is the same as that of the program, so the container cannot be released before the program ends, resulting in Memory leakage;

For example:

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


O The object is no longer needed, but cannot be recycled

(2) Various connection problems: database connections and I/O connections need to call the close method to release connections, such as connection, statement, resultset

(3) Listener usage

(4) unreasonable scope of Variables

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

Msgd should be used in some aspects.

Class server {private string msg; public void recieveMsg () {readMsg (); // obtain msg savaDB (); // input db }}

2) the object is not set to null in time

Or set msg to null after use.

(5) Use of Singleton Mode

Class BigClass {...} classSingleton {private BigClass bc; ...... // The bc object cannot be recycled.

 

Garbage collection algorithms include:

(1) reference counting algorithm

(2) tracking and recycling algorithm, reference diagram

(3) compression and recycling Algorithm

(4) copy and recycle Algorithms

(5) collection by Generation Algorithm

 

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.