Java中会不会发生内存泄漏 [英] Can there be memory leak in Java

查看:88
本文介绍了Java中会不会发生内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次问到这个问题.什么是答案的好方法

I get this question asked many times. What is a good way to answer

推荐答案

Java中会不会发生内存泄漏?

Can there be memory leak in Java?

答案是,这取决于您正在谈论的是哪种内存泄漏.

The answer is that it depends on what kind of memory leak you are talking about.

经典的C/C ++内存泄漏是在应用程序对对象完成后忽略freedispose时发生的,并且会泄漏.循环引用是这种情况的一个子情况,其中应用程序很难知道何时执行free/dispose,因此忽略了执行.相关问题是应用程序在释放对象后使用对象,或尝试释放对象两次. (您可以将后一个问题称为内存泄漏,或者仅是错误.无论哪种方式……)

Classic C / C++ memory leaks occur when an application neglects to free or dispose an object when they are done with it, and it leaks. Cyclic references are a sub-case of this where the application has difficulty knowing when to free / dispose, and neglects to do it as a result. Related problems are where the application uses an object after it has been freed, or attempts to free it twice. (You could call the latter problems memory leaks, or just bugs. Either way ... )

Java和其他(完全 1 )种托管语言主要不会遇到这些问题,因为GC负责释放不再可访问的对象. (当然,不存在悬空指针和双重释放问题,对于C/C ++智能指针"和其他引用计数方案而言,循环也不成问题.)

Java and other (fully1) managed languages mostly don't suffer from these problems because the GC takes care of freeing objects that are no longer reachable. (Certainly, dangling pointer and double-free problems don't exist, and cycles are not problematic as they are for C / C++ "smart pointers" and other reference count schemes.)

但是在某些情况下,Java中的GC会丢失对象(从程序员的角度来看)应该被垃圾回收的对象.当GC无法确定无法访问对象时,就会发生这种情况:

But in some cases GC in Java will miss objects that (from the perspective of the programmer) should be garbage collected. This happens when the GC cannot figure out that an object cannot be reached:

  • 程序的逻辑/状态可能使得使用某些变量的执行路径不会出现.开发人员可以看到这很明显,但是GC不能确定,并且在谨慎方面会犯错误(这是必需的).
  • 程序员对此可能有误,并且GC正在避免可能导致引用悬空的内容.

(请注意,Java中内存泄漏的原因可能很简单,也可能很微妙;请参见@ jonathan.cone的一些微妙答案.最后一个可能涉及您不应该仍要依靠GC进行处理.)

(Note that the causes of memory leaks in Java can be simple, or quite subtle; see @jonathan.cone's answer for some subtle ones. The last one potentially involves external resources that you shouldn't rely on the GC to deal with anyway.)

无论哪种方式,您都可能遇到无法垃圾收集不需要的对象的情况,并四处闲逛占用内存……内存泄漏.

Either way, you can have a situation where unwanted objects cannot be garbage collected, and hang around tying up memory ... a memory leak.

然后就是Java应用程序或库可以通过需要手动管理的本机代码分配堆外对象的问题.如果应用程序/库有错误或使用不正确,则可能会发生本机内存泄漏. (例如: Android位图内存泄漏 ...注意到此问题已在更高版本中得到解决.的Android.)

Then there is the problem that a Java application or library can allocate off-heap objects via native code that need to be managed manually. If the application / library is buggy or is used incorrectly, you can get a native memory leak. (For example: Android Bitmap memory leak ... noting that this problem is fixed in later versions of Android.)

1-我在暗示两件事.某些托管语言允许您编写非托管代码,在其中可以创建经典的存储泄漏.其他一些托管语言(或更准确地说是语言实现)使用引用计数而不是适当的垃圾收集.基于引用计数的存储管理器需要某些东西(即应用程序)来中断周期……否则将导致存储泄漏.

这篇关于Java中会不会发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆