The Introduction of Java Memory Leaks
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.
1. What is Memory Leak?
Definition of Memory Leak: objects are no longer being 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.
2. 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.

3. How to Prevent Memory Leaks?
The following are some quick hands-on tips for preventing memory leaks.
- 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.
- 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.
- "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.
4. 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 Java Memory Leaks的更多相关文章
- Find out when memory leaks are a concern and how to prevent them
		Handling memory leaks in Java programs Find out when memory leaks are a concern and how to prevent t ... 
- On Memory Leaks in Java and in Android.
		from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mea ... 
- Diagnosing out of memory errors and memory leaks  内存泄露实例 C Java JavaScript 内存泄露
		小结: 1. 数据库连接池. JDBC语句和结果对象必须显式地关闭. 2. 电梯到目标楼层后地址是否被释放 When a button is pressed: Get some memory, whi ... 
- Debugging Java Native Memory Leaks
		GZIP造成JAVA Native Memory泄漏案例 https://www.elastic.co/blog/tracking-down-native-memory-leaks-in-elasti ... 
- Java Memory Management(1)
		Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ... 
- 内存泄露 Memory Leaks
		什么是内存泄露 内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Java的垃圾收集器将会自动管理内存空间的分配和释放. 但在很多情况下,事情并不那么简单,在 Java程序中总是 ... 
- Activitys, Threads, & Memory Leaks
		Activitys, Threads, & Memory Leaks 在Android编程中,一个公认的难题是在Activity的生命周期如何协调长期运行的任务和避免有可能出现的内存泄漏问题. ... 
- Is there a way to avoid undeployment memory leaks in Tomcat?
		tomcat 项目部署问题 - yshy - 博客园http://www.cnblogs.com/yshyee/p/3973293.html jsp - tomcat - their classes ... 
- 内存泄漏 Memory Leaks 内存优化 MD
		Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ... 
随机推荐
- js各类共用方法
			function GetParameterValueByName(parametername) { var reg = new RegExp("(^|&)" + param ... 
- gif修改背景透明
			1.用ImageReady打开,将选中所有帧,右键选择“恢复为背景”. 2.打开“颜色板”,点击左下角的惊叹号,用吸色器点击背景,颜色板自动选中了背景色,将其映射为透明. 3.文件->将优化结果 ... 
- EHCache 实现通用类  CacheManager
			package com.zhubaje.api.workflow.ehcache; import java.io.Serializable; import java.util.ArrayList; i ... 
- Window.Open参数、返回值
			一.window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法: window.open(pageURL,name, ... 
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程
			生命在于折腾. 这个帖子作为我安装xilinx ISE 14.7版本一个记录.希望给需要的人一些帮助,这些内容绝大部分也是来源于互联网. 软硬件: lsb_release -a No LSB modu ... 
- iOS 10 版本适配问题收集-b
			随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,欢迎指出. 1.系统判断方法失效: 在你的项目中,当需要判断系统版本的话,不要使 ... 
- 30款jQuery常用网页焦点图banner图片切换 下载
			1.jquery 图片滚动特效制作 slide 图片类似窗帘式图片滚动 查看演示 2.jquery幻灯片插件带滚动条的圆形立体图片旋转滚动 查看演示 3.jQuery图片层叠旋转类似洗牌翻转图片幻灯片 ... 
- SHELL syntax error:unexpected end of file 提示错误
			SHELL syntax error:unexpected end of file 提示错误 if [ -n "$1" ] then " else " fi e ... 
- mysql数据库连接池 手动编写
			源码来源于http://www.toutiao.com/a6350448676050174209/,留存以供以后参考学习 先上一张项目托普图 然后分别列出各个文件的源码: MyPool.java(就是 ... 
- [bzoj 2159]Crash的文明世界
			今天看到一个鬼题 心情好的时候写 [题意]求树上所有点对距离的k次方和,所有边权为1 大爷方的题解:http://tonyfang.is-programmer.com/posts/204972.htm ... 
