java 如何在android中释放对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14016732/
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
How to free object in android?
提问by barssala
Possible Duplicate:
Java garbage collection
可能的重复:
Java 垃圾收集
I know that we have to free object in C, C++ after processing finish to get back the memory. However, I don't know how to free object in java and android. Is that enough for just assign null
to the object?
我知道我们必须在处理完成后释放 C、C++ 中的对象以取回内存。但是,我不知道如何在 java 和 android 中释放对象。仅分配null
给对象就足够了吗?
回答by Matt Clark
In Java it is un-necessary to free objects.
在 Java 中,没有必要释放对象。
Java has a built in Garbage Collector which runs when ever it needs to and clear out all resources that are no longer in use in order to free memory. A java developer may make a call to the java runtime to run the Garbage Collecter using System.gc();
however this is just a suggestion to the runtime and may not always result in it being run.
Java 有一个内置的垃圾收集器,它在需要时运行并清除所有不再使用的资源以释放内存。java 开发人员可以调用 java 运行时来运行垃圾收集器,System.gc();
但这只是对运行时的建议,可能并不总是导致它运行。
In cases where you are using readers and images, be sure to call .recycle()
and .close()
where applicable.
如果您使用阅读器和图像,请务必致电.recycle()
并.close()
在适用的情况下致电。
回答by Matt Clark
A simple java object especially (E.g. model objects) can be freed by garbage collector IFother objects has no reference to it.
一个简单的java对象(例如模型对象)可以被垃圾收集器释放,如果其他对象没有引用它。
If I were you, don't trust too much that garbage collector because there are some objects that you must free, one of them is the Bitmap
objects
如果我是你,不要太相信垃圾收集器,因为有些对象你必须释放,其中之一就是Bitmap
对象
Bitmap
s eat more RAM in your android app.
Bitmap
在你的安卓应用中吃更多的内存。
Bitmap b = createLargeBitmap();
Bitmap b2 = b;
If you remove all references to that object and let garbage collector kill it
如果您删除对该对象的所有引用并让垃圾收集器杀死它
b = null;
b2 = null;
you might get a memory leak or OutOfMemory
error.
您可能会遇到内存泄漏或OutOfMemory
错误。
So you need to call recycle()
to fully freed the bitmap.
所以你需要调用recycle()
来完全释放位图。
b.recycle();
b = null;
b2 = null;
// Sorry for my wrong grammar :)
// 抱歉我的语法错误:)
回答by Mudassir Hasan
Memory deallocation is automatically done by Java garbage collector . You can't force garbage collector to free memory through your code.
内存释放由 Java 垃圾收集器自动完成。你不能强制垃圾收集器通过你的代码释放内存。
Calling System.gc()
doesnot guarantee garbage collector to RUN and FREEmemory , final decision is taken by Java runtime.
调用System.gc()
并不能保证垃圾收集器运行并释放内存,最终决定由 Java 运行时做出。
回答by wtsang02
回答by gerrytan
Android (which uses Java language) is a garbage-collected environment, meaning the virtual machine will automatically remove objects which no longer have any references.
Android(使用 Java 语言)是一个垃圾收集环境,这意味着虚拟机将自动删除不再有任何引用的对象。
Hence the question you should be asking is: how do you ensure your program does not use too much memory. This is normally achieved by ensuring you don't put too many objects in your in-memory data structure, persist your information into file system etc.
因此,您应该问的问题是:您如何确保您的程序不会使用太多内存。这通常是通过确保您不会在内存数据结构中放入太多对象、不会将您的信息保存到文件系统等来实现的。