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.

  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.

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Diagnosing out of memory errors and memory leaks 内存泄露实例 C Java JavaScript 内存泄露

    小结: 1. 数据库连接池. JDBC语句和结果对象必须显式地关闭. 2. 电梯到目标楼层后地址是否被释放 When a button is pressed: Get some memory, whi ...

  4. Debugging Java Native Memory Leaks

    GZIP造成JAVA Native Memory泄漏案例 https://www.elastic.co/blog/tracking-down-native-memory-leaks-in-elasti ...

  5. Java Memory Management(1)

    Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...

  6. 内存泄露 Memory Leaks

    什么是内存泄露 内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Java的垃圾收集器将会自动管理内存空间的分配和释放. 但在很多情况下,事情并不那么简单,在 Java程序中总是 ...

  7. Activitys, Threads, & Memory Leaks

    Activitys, Threads, & Memory Leaks 在Android编程中,一个公认的难题是在Activity的生命周期如何协调长期运行的任务和避免有可能出现的内存泄漏问题. ...

  8. 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 ...

  9. 内存泄漏 Memory Leaks 内存优化 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. XSS获取cookie

    在你服务器的html目录下创建joke文件夹: 在joke文件夹中创建joke.js 和joke.php joke.js 创建img标签,将它的src属性指向另一个脚本joke.php,这里关键的一点 ...

  2. hdu 1222 狼和兔子

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  3. ASP.NET对HTML元素进行权限控制(二)

    这是这个权限控制的第一步,扫描界面把要分配权限的元素的信息获取出来存入到数据库中. 这一步分三小步: (1).标出界面所要分配权限的元素 (2).扫描界面获取所要分配权限的元素信息.(ID,标题,层级 ...

  4. SQL多表连接

    在KS系统中分配好权限以后,在用户登录的时候就要通过用户查到角色,通过角色查到界面,界面又属于某个菜单,一共要查4个表.并且不能有重复的记录这个时候就用到了SQL的内连接.SQL的多表连接很方便,以前 ...

  5. MySQL Online DDL 工具之pt-online-schema-change

    MySQL DDL:DDL是一个令所有MySQL dDBA 诟病的一个功能,因为在MySQL中在对表进行dDDL时,会锁表,当表比较小比如小于1W行时,对前端影响较小,当时遇到千万级别的表,就会影响前 ...

  6. 基于UUID生成短ID

    为什么需要短ID 数据库操作过程最常用到: 自增ID UUID 前者多数依赖Mysql的auto_increment,但数据移植麻烦. 如果是主从或主主,不同库里自增ID还可能不一致. 后者长度是个问 ...

  7. Reactor模式

    对象行为类的设计模式,对同步事件分拣和派发.别名Dispatcher(分发器) Reactor模式是处理并发I/O比较常见的一种模式,用于同步I/O,中心思想是将所有要处理的I/O事件注册到一个中心I ...

  8. ASP.NET MVC 学习第三天

    今天来简单说一下Razor视图引擎语法相关的和视图类. 添加一个MvcTest项目,继续添加一个Home控制器,完成index的视图添加.我们就在index这里分析razor视图引擎.下面是home控 ...

  9. html onclick 传参数

    <a id="j-im" class="jd-im btn-gray gys-im" href="javascript:(0);" o ...

  10. iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述

    本文档尝试用Video Toolbox进行H.265(HEVC)硬件编码,视频源为iPhone后置摄像头.去年做完硬解H.264,没做编码,技能上感觉有些缺失.正好刚才发现CMFormatDescri ...