Android中如何释放内存

发布于 2024-12-10 02:22:11 字数 170 浏览 0 评论 0原文

我需要释放下面的指针,如何释放画布、油漆和矩阵的内存?

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新一帅帅 2024-12-17 02:22:11

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().

十二 2024-12-17 02:22:11

当您不再引用您创建的对象后,为其分配 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文