文档章节

浅谈Java内存泄漏问题

嘎吱船长
 嘎吱船长
发布于 2016/12/16 15:08
字数 445
阅读 22
收藏 1

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.

where-is-memory-leak

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.

object-life-time

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.

References:
[1] Bloch, Joshua. Effective java. Addison-Wesley Professional, 2008.
[2] IBM Developer Work. http://www.ibm.com/developerworks/library/j-leaks/

© 著作权归作者所有

共有 人打赏支持
嘎吱船长
粉丝 5
博文 9
码字总数 7849
作品 0
广州
高级程序员
私信 提问
JAVA内存的“防水补漏”解决方案

Java内存泄漏是每个Java程序员都会遇到的问题,程序在本地运行一切正常,可是布署到远端就会出现内存无限制的增长,最后系统瘫痪,那么如何最快最好的检测程序的稳定性,防止系统崩盘,作者用...

loki_lan
2013/04/09
829
6
ThreadLocal内存泄漏的问题

最近看了些ThreadLocal内存泄漏的文章,发现ThreadLocal内存泄漏的原因是因为他里面有个ThreadLocalMap中的Entry的Key 是使用WeakReference进行的弱引用,可能会导致GC回收。 那么问题来了,一...

_乌托邦
11/15
89
2
java.lang.OutOfMemoryError: Java heap space

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 今天我想说说这个有趣的问题,这的确是个让人费解的是error 问题来源: 这个问题一般会出在myecplise 和ecplise ,...

soul_mate
2014/05/03
0
0
Confluence 6 内存使用和需求和一些问题

系统备份和恢复 Confluence 的备份和恢复是与数据库中数据量的大小有关。这个操作可能会对 Confluence 的性能产生很多关键性的影响并且大量消耗内存。如果你在 Confluence 的系统备份和恢复过...

honeymose
09/17
0
0
【译】关于 “Tomcat 7 内存泄漏保护” 的访谈

今年年初,DZone 网站对 Mark Thomas 针对 Tomcat 7 的内存泄漏保护的功能进行了采访,Mark Thomas 是 SpringSource 的首席软件工程师,同时也是 Tomcat 的代码提交负责人。 在 Tomcat 7 中引...

鉴客
2010/06/09
1K
5

没有更多内容

加载失败,请刷新页面

加载更多

easyui tree

<tr> <th><spring:message code="wf.borrow.examiner"/></th> <td> <input id="inp-examiner1" type="text" name="examiner1" style="width:197px;height:20px;" data-options="required:tru......

小兵胖胖
6分钟前
0
0
内存性能的正确解读

一台服务器,不管是物理机还是虚拟机,必不可少的就是内存,内存的性能又是如何来衡量呢。 1. 内存与缓存 现在比较新的CPU一般都有三级缓存,L1 Cache(32KB-256KB),L2 Cache(128KB-2MB)...

阿里云云栖社区
9分钟前
0
0
微服务架构:Zuul 1.0 和 2.0 我们该如何选择?

在今年5月中,Netflix终于开源了它的支持异步调用模式的Zuul网关2.0版本,真可谓千呼万唤始出来。从Netflix的官方博文[附录1]中,我们获得的信息也比较令人振奋: The Cloud Gateway team a...

大木老师故事的小黄花
9分钟前
0
0
基础掌握

哪些是基础功呢?我觉得包括: 数据结构和算法:链表、队列、栈、堆、树(RBT, B/B+)、跳表、哈希、图;查找(二分、bst)、排序(冒泡、插入、快排、归并、堆排、希尔)、递归、归并、回溯、...

边鹏_尛爺鑫
11分钟前
0
0
Android APP的安装路径

一. Android应用安装路径有两种情况: system/app 系统自带的应用程序,无法删除。root后可以删除,注意可能造成系统崩溃,不过有的垃圾捆绑软件只能这么删除了 data/app 用户程序安装的目录,...

天王盖地虎626
14分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部