pavan
pavan

Reputation: 1135

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.

Upvotes: 1

Views: 11171

Answers (2)

spatulamania
spatulamania

Reputation: 6663

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

Upvotes: 5

Blackbelt
Blackbelt

Reputation: 157447

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.

Upvotes: 2

Related Questions