简体   繁体   中英

How to release memory in android

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.

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

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 ac/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM