java Android 正确清理/处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6698867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:57:08  来源:igfitidea点击:

Android proper clean up/disposing

javaandroidgarbage-collection

提问by semajhan

Is there a way to "clean up" objects and other variables you create? Or are they automatically disposed of or do I have this whole concept wrong? What is the proper way to go about doing this? I am trying to avoid the GC as much as possible.

有没有办法“清理”您创建的对象和其他变量?或者它们是自动处理的还是我整个概念都错了?这样做的正确方法是什么?我试图尽可能地避免 GC。

回答by Ravi Vyas

The only way to cleanup in an GC language with no memory management is the GC . You can force GC but its not recommended , the GC is pretty good , to be more proactive set objects to null for the GC to clean up.

在没有内存管理的 GC 语言中进行清理的唯一方法是 GC 。您可以强制 GC 但不推荐,GC 非常好,更主动地将对象设置为 null 以便 GC 清理。

Addition:

添加:

Also try to make objects as local as possible , that way they are GCed as they scope out.

还尝试使对象尽可能本地化,这样它们在范围外就被 GC 处理。

回答by Chris Lucian

Calling System.gc()will force Garbage Collection to happen.

调用System.gc()将强制垃圾收集发生。

There is a system counting references to objects you create. If you are looping a lot and creating lots of objects you will create periods of time where they pile up. The system will collect the garbage when your processor is not doing anything, or it will wait till you need more free memory before collection occurs. If you have been processing for some time, you will experience hiccups in your performance due to Garbage Collection happening during your processes.

有一个系统计算对您创建的对象的引用。如果您要循环很多次并创建很多对象,那么您将创建它们堆积的时间段。当您的处理器不执行任何操作时,系统将收集垃圾,或者在收集发生之前等待您需要更多可用内存。如果您已经处理了一段时间,由于在您的处理过程中发生垃圾收集,您会遇到性能打嗝。

Please view this page and search for "Garbage Collection"

请查看此页面并搜索“垃圾收集”

http://developer.android.com/guide/practices/design/performance.html

http://developer.android.com/guide/practices/design/performance.html

NOTE: Anything created with an Application Context will live until the end of the application execution. Anything created with an Activity Context will live until the end of the activity. This two situations can cause memory leaks!

注意:使用应用程序上下文创建的任何内容都将持续到应用程序执行结束。使用 Activity Context 创建的任何内容都将一直存在到 Activity 结束。这两种情况都会导致内存泄漏!

回答by Codeman

For a more complete answer specific to Android:

有关特定于 Android 的更完整答案:

Make sure you review the application lifecyclefor android. It will help you avoid activity leaks in Android.

确保您查看了android的应用程序生命周期。它将帮助您避免 Android 中的活动泄漏。

回答by Maggie

Android's activities have onDestroy() method. You can use this method to close open connections or dialogs or close some pending tasks.
You could also read about Java GC to get a more proper understanding of it. I would recommend SCJP book, Garbage collection chapter. It explains well when an object becomes eligible for garbage collection.

Android 的活动有 onDestroy() 方法。您可以使用此方法关闭打开的连接或对话框或关闭一些挂起的任务。
您还可以阅读有关 Java GC 以更正确地理解它。我会推荐 SCJP 书,垃圾收集章节。它很好地解释了对象何时符合垃圾收集条件。

回答by ahodder

For the most part they are cleaned up as long as you do not maintain a reference to the object (variable). Something's like cursor's and bitmap's though need to be closed before they can be deleted to prevent memory leaks.

在大多数情况下,只要您不维护对对象(变量)的引用,它们就会被清除。像cursor's 和bitmap's之类的东西在删除之前需要关闭,以防止内存泄漏。

I don't think you have to worry about the GC as long as your object creation is not over the top. Note: GC is a part of java. You can't avoid it.

我认为只要您的对象创建不超过顶部,您就不必担心 GC。注意:GC 是 java 的一部分。你无法避免。

Addendum 1: If you really are that worried about it, you could reuse variables. That way you keep object creation to a minimum, but in so doing you will lose that variable and will be unable to store a wide range of data.

附录 1:如果你真的那么担心,你可以重用变量。这样,您可以将对象创建保持在最低限度,但这样做会丢失该变量,并且无法存储大量数据。