参考文章:
malloc/free和new/delete必须成对出现,以防止内存泄露
一、什么时候垃圾回收:
简单说:
当一块内存被jvm通过它自己的认证机制认为不再被调用的时候才会在
它认为合适的时机进行回收;
具体说:
The job of the garbage collector is to find objects that are no longer needed by an application and to remove them when they can no longer be accessed or referenced. The garbage collector starts at the root nodes, classes that persist throughout the life of a Java application, and sweeps through all of the nodes that are referenced. As it traverses the nodes, it keeps track of which objects are actively being referenced. Any classes that are no longer being referenced are then eligible to be garbage collected. The memory resources used by these objects can be returned to the Java virtual machine (JVM) when the objects are deleted.
So it is true that Java code does not require the programmer to be responsible for memory management cleanup, and that it automatically garbage collects unused objects. However, the key point to remember is that an object is only counted as being unused when it is no longer referenced.
垃圾收集器的工作是找到由一个应用程序不再需要的对象,在他们不再被访问或引用将其删除。垃圾收集器从根节点、在整个Java应用的生命中存在的类开始,并通过扫描所有被引用的节点。由于它遍历的节点,它跟踪哪些对象正在积极引用。任何不再被引用的的类,然后才有资格被垃圾收集。当对象被删除时,他们所占用的内存资源,才被Java虚拟机(JVM)回收。
二、什么样的java代码容易memory leak?
1.首先一种情况是collection或者是map一直被put数据,没有机会remove,导致OutOfMemoryError。尤其是当collection或者是map被设计成static变量的时候,它就是个global性质的变量,很可能永远不会被赋为null。这也是不建议使用static变量的一个原因。
2.
在listener的模式下,如果listener一直在注册register而没有机会remove也会导致OutOfMemoryError。其实listener也是一个list的结构,本质上是一样的。很多listener是以匿名类被构造和注册到被监听类上面去的, 而被监听类如果也没有正确remove注册的listener的话也会导致OutOfMemoryError。
待续...........
分享到:
相关推荐
This program is a tool that help you positioning memory leak locations of an QUALCOMM BREW application on simulator. It can give the call stack of memory leaks (including the locations that MALLOC/...
内存泄漏是程序在申请内存后,无法释放已申请的内存空间,一次小的内存泄漏可能看似无足轻重,但随着时间推移,大量的内存泄漏会消耗掉系统可用内存,导致性能下降甚至系统崩溃。在C++编程中,由于手动管理内存的...
**BREW Memory Leak Checker** 是一个专为BREW(Binary Runtime Environment for Wireless)平台设计的工具,用于检测和定位BREW应用程序中的内存泄漏问题。在移动设备开发中,内存管理是至关重要的,因为有限的资源...
本文将深入探讨C++中的内存泄漏问题及其解决策略,并介绍两种常用的内存泄漏检测工具——Visual Leak Detector (VLD) 和 CodeSnitch。 内存泄漏的发生通常是由于以下原因: 1. 动态分配的内存未被释放:当使用`new`...
BREW Memory Leak Checker是一款专为BREW(Binary Runtime Environment for Wireless)平台设计的内存泄漏检测工具,版本号为20111030。该工具的主要功能是在BREW模拟器上帮助开发者定位并解决应用程序中的内存泄漏...
BREW Memory Leak Checker是一款专为BREW(Binary Runtime Environment for Wireless)平台设计的内存泄漏检测工具,版本号为20111109。该工具的主要功能是在BREW模拟器上帮助开发者定位并解决应用程序中的内存泄漏...
Glassfish 3.1.2.2 Web Service Memory Leak Workaround Glassfish 3.1.2.2 中的 Memory Leak 问题可能会导致服务器变慢或无响应。这种问题可能是由于 Web Service 的 Memory Leak 导致的。在此文中,我们将讨论...
在C++编程中,内存泄漏是一个常见的问题,它发生在程序员分配了内存但未能正确地释放。这可能导致程序占用越来越多的内存,最终导致性能下降甚至崩溃。为了帮助开发者检测和修复内存泄漏,本压缩包提供了一个内存...
JavaScript是一种广泛应用于Web开发的脚本语言,它在执行时占据内存资源,如果代码编写不当,可能会出现“内存泄漏”(Memory Leak)的问题。内存泄漏是程序运行时未释放不再使用的内存,导致系统资源浪费,严重时...
This program is a tool that help you positioning memory leak locations of an QUALCOMM BREW application on simulator. It can give the call stack of memory leaks (including the locations that MALLOC/...
Memory Leak(解决方案).md
"Android中Memory Leak原因分析及解决办法" Android中Memory Leak原因分析及解决办法是Android开发过程中的重要问题之一。Memory Leak是指在Android应用程序中,一些对象不能被正确释放,导致这些对象继续占用内存...
Memory Leak Detected(亲测可用).md
编译器设计之代码分析工具:Memory Leak Detectors
1. `_CrtSetDbgFlag`: 这个函数允许我们设置调试标志,其中`_CRTDBG_LEAK_CHECK_DF`是用于开启内存泄漏检查的标志。通过在程序启动时调用`_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF)`,我们可以让程序在退出时报告内存...
内存泄漏检测器 MemoryLeakDetector是由Xigua视频android团队开发的本地内存泄漏监视工具。 它具有访问简单,监视范围广,性能...leak-detector:0.0.7 '} 步骤3:添加代码以方便使用(使用广播控件不需要此步骤) //
"Visual C++ Memory Leak Detector"就是针对这类问题的工具,它可以帮助开发者检测和定位C++程序中的内存泄漏。 该压缩包文件包含了一个名为"vld-1.9d-setup.exe"的安装程序,这很可能是Visual Leak Detector (VLD)...
首先,我们来了解一下什么是内存泄漏(Memory Leak)。内存泄漏是指程序在申请内存后,无法释放已不再使用的内存空间,一次小的内存泄漏可能看似无足轻重,但随着时间推移,大量累积的内存泄漏会消耗掉系统资源,...