文档章节

Java内存泄漏管理

豆奶
 豆奶
发布于 2013/12/27 09:11
字数 594
阅读 20
收藏 0

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 are Memory Leaks?

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.

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/

Related posts:

  1. What do Java objects look like in memory during run-time?

  2. What does a Java array look like in memory?

  3. How is an array stored in memory and brought to cache?

  4. AMD versus Intel: Successes and pitfalls of their processor architectures – (1) Abstract and Introduction

本文转载自:http://www.programcreek.com/2013/10/the-introduction-of-memory-leak-what-why-and-how/

共有 人打赏支持
豆奶
粉丝 0
博文 3
码字总数 241
作品 0
大连
程序员
私信 提问
JAVA内存的“防水补漏”解决方案

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

loki_lan
2013/04/09
829
6
Java 内存管理原理、内存泄漏实例及解决方案研究

Java 内存管理原理、内存泄漏实例及解决方案研究 在项目的最后阶段,就是要防止系统的内存泄漏了,顺便找了些资料,看了些java内存泄漏的实例及解决,总结一下: Java是如何管理内存 为了判断...

gavin_jin
2012/02/29
0
0
在 JNI 编程中避免内存泄漏

此文转自 IBM developerWorks JNI 编程简介 JNI,Java Native Interface,是 native code 的编程接口。JNI 使 Java 代码程序可以与 native code 交互——在 Java 程序中调用 native code;在...

IBMdW
2011/04/26
968
1
十个最好的Java性能故障排除工具

推荐十个最好的Java性能故障排除工具: 1.jconsole是随着JDK 1.5而推出的。这是一个Java监测和管理控制台-JMX兼容的图形工具来监测Java虚拟机。它能够同时监测本地和远程的JVMs。详情可查看:...

JavaGG
2009/07/18
8.9K
12
ActiveMQ : Async error occurred: java.lang.OutO...

参考--http://activemq.apache.org/javalangoutofmemory.html 对于MQ的内容实用是可管理和可配置的。首先需要判断的是MQ的哪部分系统因内存不足而导致泄漏,是JVM,broker还是消费者、生产者...

hbdrawn
2011/06/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

mogodb服务

部署MongoDB 官网: https://www.mongodb.com/download-center/community 创建mongo数据目录 mkdir /data/mongodb 二进制部署 wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x8......

以谁为师
昨天
1
0
大神教你Debian GNU/Linux 9.7 “Stretch” Live和安装镜像开放下载

Debian项目团队于昨天发布了Debian GNU/Linux 9 "Stretch" 的第7个维护版本更新,重点修复了APT软件管理器中存在的安全漏洞。在敦促每位用户尽快升级系统的同时,Debian团队还发布了Debian ...

linux-tao
昨天
3
0
PHP 相关配置

1. php-fpm的pool 编辑php-fpm配置文件php-fpm.con vim /usr/local/php/etc/php-fpm.conf //在[global]部分增加以下内容 include = etc/php-fpm.d/*.conf # 相当与Nginx的虚拟主机文件 “vho......

Yue_Chen
昨天
2
0
EOS主网数据同步指南

本文介绍如何安装EOS节点软件并接入EOS主网,主要包括以下内容: 如何安装EOS节点软件 如何配置EOS节点软件接入主网 如何启动EOS节点软件与主网数据同步 如何检查主网数据同步进度 如何正确地...

汇智网教程
昨天
3
0
matlab-线性代数 齐次方程组 基础解系和通解

  matlab : R2018a 64bit     OS : Windows 10 x64 typesetting : Markdown    blog : my.oschina.net/zhichengjiu    gitee : gitee.com/zhichengjiu   code clearclc% x1+2*......

志成就
昨天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部