Note the Java code Memory leakage

Source: Internet
Author: User

Original article:

Http://blog.csdn.net/yahuadai/article/details/7675318

Attention should be paid to the comrades who have language compilation basics. if you have just learned Java. you must check the following. of course, the same is true for me. the following errors may also occur. so I have summarized some.

Of course, they are all personal opinions. no, it is correct. maybe something is wrong. at the same time, I hope that the experts who are familiar with Java will point out. or have better opinions or suggestions. can communicate with me. let's learn together.
At the same time, this is also an improvement for yourself.

In the project application. complex services are often faced. in addition, the number of users has increased. memory usage will naturally increase. there are also HTTP servers that only have certain capabilities. therefore, code writing is also very important. performance and stability. this is extremely important.

Summary:

1. Release reference of useless objects as soon as possible. Especially for large objects and collection objects, if this parameter is set to null, the garbage collector is used to collect the object to prevent memory leakage.
2. If a large number of strings are processed in the program, use stringbuffer instead of string.
For (INT I = 0; I <100; I ++)
{
String S1 = "";
S1 = S1 + "ABC"; // do not write like this
}

3. Use as few static variables as possible, because static variables are global and GC will not be recycled.

4. Avoid creating objects in a centralized manner, especially large objects. JVM will suddenly need a large amount of memory, which will inevitably trigger GC to optimize the system memory environment. The declared array space is displayed, and the number of applications is large.
5. Try to use the object pool technology to improve system performance. objects with a long life cycle have objects with a short life cycle, which may cause memory leakage, for example, when a large collection object has a large data volume of business objects, you can consider processing them in multiple parts and then solve the policy of releasing one piece.

6. Do not create objects in frequently called methods, especially avoid creating objects in loops. You can use hashtable as appropriate. The vector creates a group of object containers and then retrieves those objects from the containers without dropping them after each new operation.

7. It usually occurs when a large file is opened or too much data is taken with the database at a time, resulting in an out of memory error. In this case, the maximum data volume is calculated, set the minimum and maximum memory space values.
8. Pay attention to the collection data types, including arrays, trees, graphs, and linked lists. The collection of these data structures is more complex for GC.
9. Avoid creating and initializing a large number of objects in the default constructor of the class, and avoid unnecessary memory resource waste when calling its self-class constructor.
10. Try to avoid forcing the system to recycle garbage memory and increase the final time for the system to recycle garbage.

11. Is there an endless loop or recursive call in the code?
12. Whether a large loop repeatedly generates new object objects.
13. Check whether the list, map, and other collection objects are cleared after use. List, map, and other collection objects always have references to objects, so that these objects cannot be recycled by GC.
The following is a simple example of Memory leakage. In this example, we cyclically apply for an object and put the applied object into a vector. If we only release the reference, the vector still references the object, therefore, this object cannot be recycled for GC.

Vector v=new Vector(10);for (int i=1;i<100; i++){    Object o=new Object();    v.add(o);    o=null; }

 
In the function, if you can clear, clear the objects in the set.

Map<Object,Object> v=new Map<Object,Object>();for (int i=1;i<100; i++){    Object o=new Object();    v.put(i,o); }v.clear();

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.