Android中如何释放内存
我需要释放下面的指针,如何释放画布、油漆和矩阵的内存?
Canvas pcanvas = new Canvas();
Paint mPaint = new Paint();
Matrix matrix = new Matrix();
提前致谢。
I need to free below pointers, how to release memory for canvas, paint and matrix?
Canvas pcanvas = new Canvas();
Paint mPaint = new Paint();
Matrix matrix = new Matrix();
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java没有指针,你不能显式释放内存。
但是,Java 和 Android 中可能会泄漏内存 。如果您的任何对象引用非托管内存,您需要让它们知道何时可以释放该内存。
因为您似乎正在做与图形相关的工作,所以我猜想您在某处使用位图,并且这些位图使用大量内存,并且需要释放内存。使用完位图后,请确保调用
Bitmap.recycle()
。Java does not have pointers and you cannot explicitly release memory.
However it is possible to leak memory in Java and Android. If any of your objects reference unmanaged memory, you need to let them know when they can release that memory.
Because it appears that you're doing graphics related work, I'd guess that you're using Bitmaps somewhere and these use a lot of memory and the memory needs to be released. When you're finished using a bitmap, make sure you call
Bitmap.recycle()
.当您不再引用您创建的对象后,为其分配 null 值。显然,这不会立即释放内存,而是通过这种方式告诉 GC 该对象有资格进行垃圾回收。(有效的 java)。
编辑:正如@spatulamania所说,如果你正在管理位图,你必须调用回收(我认为它调用ac/c++ free)方法才能释放与之相关的内存。设置为 null 位图引用没有用,因为位图是本机实现的并且 java 对象只有几个字节。
after you are not more referencing the object you created, assign it the null value. That obviously does not release immediately the memory but int this way you are telling to the GC that the object is eligible for garbage collection.. (effective java).
Edit: as @spatulamania said, if you are managing bitmap you have to call the recycle (it call a c/c++ free I think) method in order to free memory associated with. Set to null a bitmap reference is not useful because the bitmap is native implemented and the java object is few bytes.