The Introduction of Memory Leaks
来源:互联网 发布:ubuntu skype 4.3 编辑:程序博客网 时间:2024/06/15 19:22
One of the most significant advantages of Java is its memory management. You simply create objects and Java Garbage Collector takes care of allocating and freeing memory. However, the situation is not as simple as that, because memory leaks frequently occur in Java applications.
This tutorial illustrates what is memory leak, why it happens, and how to prevent them.
What are Memory Leaks?
Definition of Memory Leak: if objects are no longer used by the application, but Garbage Collector can not remove them because they are being referenced.
To understand this definition, we need to understand objects status in memory. The following diagram illustrates what is unused and what is unreferenced.
From the diagram, there are referenced objects and unreferenced objects. Unreferenced objects will be garbage collected, while referenced objects will not be garbage collected. Unreferenced objects are surely unused, because no other objects refer to it. However, unused objects are not all unreferenced. Some of them are being referenced! That’s where the memory leaks come from.
Why Memory leaks happen?
Let’s take a look at the following example and see why memory leaks happen. In the example below, object A refers to object B. A’s lifetime (t1 – t4) is much longer than B’s (t2 – t3). When B is no longer being used in the application, A still holds a reference to it. In this way, Garbage Collector can not remove B from memory. This would possibly cause out of memory problem, because if A does the same thing for more objects, then there would be a lot of objects that are uncollected and consume memory space.
It is also possible that B hold a bunch of references of other objects. Those objects referenced by B will not get collected either. All those unused objects will consume precious memory space.
How to prevent Memory Leaks?
The following are some quick hands-on tips for preventing memory leaks.
1. Pay attention to Collection classes, such as HashMap, ArrayList, etc., as they are common places to find memory leaks. When they are declared static
, their life time is the same as the life time of the application.
2. Pay attention to event listeners and callbacks. A memory leak may occur if a listener is registered but not unregistered when the class is not being used any longer.
3. “If a class manages its own memory, the programer should be alert for memory leaks.”[1] Often times member variables of an object that point to other objects need to be null out.
A little Quiz: Why substring() method in JDK 6 can cause memory leaks?
To answer this question, you may want to read Substring() in JDK 6 and 7.
References:
[1] Bloch, Joshua. Effective java. Addison-Wesley Professional, 2008.
[2] IBM Developer Work. http://www.ibm.com/developerworks/library/j-leaks/
- The Introduction of Memory Leaks
- The Introduction of Memory Leaks
- The Introduction of Memory Leaks
- Finding Memory Leaks Using the CRT Library
- Finding Memory Leaks Using the CRT Library
- The introduction of wispr
- The introduction of vbfa
- The introduction of Regex
- the introduction of the hadoop
- JavaScript and memory leaks
- Hunting Java Memory Leaks
- Avoiding memory leaks
- Avoiding memory leaks
- Avoiding memory leaks
- Avoiding Memory Leaks
- VC STLport memory leaks
- Memory leaks using CVSListBox
- iphone memory leaks检测
- IOS学习--performSelectorOnMainThread
- 内存空间的划分
- ruby Array类函数说明
- FTP服务器
- Portal技术
- The Introduction of Memory Leaks
- struts2 获取request,seesion,application对象
- js返回和前进
- Linux下全局符号覆盖问题
- Windows2003 IIS6完美解决Wordpress伪静态的方法
- 创建WCF服务项目遇到的问题以及解决方法
- Unity3D开发之利用GL画曲线
- IAR编译 Warning[Pa082]:undefined behavior: the order of volatile accesses is undefined in this stateme
- 通过JavaScript操作浏览器前进后退列表