Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8190

Out of memory because of heap memory in For loop?

Recently, I declare a function and I put the function in for loop like below:

 Bitmap image = ...//Do stuff to get image's bitmap, it's quite ok
 for(int i =0; i < someNumber; i++){
      image = doSomeThing(image, width, height);      
 }

 private Bitmap doSomeThing(image, width, height){
      int[] a = new int[10000];
      Bitmap bitmap = image.copy(sentBitmap.getConfig(), true);
      // Do some stuff here to process image
      ...
      return (bitmap);
 }

With the for loop argument someNumber is smaller than 5, my application is quite fine, otherwise, I will be outOfMemory. So I want to ask for a suggestion to resolve this exception! Can I use System.gc()? Or can I do something to remove ununsed memory after each loop of for?

Edited: Sorry, maybe I omited too much, I updated my code!

Edited2 : If instead of for loop, I use call image = doSomething(...) for multiple times, the exception is no more!!

Thanks!

Upvotes: 1

Views: 1031

Answers (2)

dmon
dmon

Reputation: 30168

You probably need to call image.recycle() when you're done with it in the for loop.

Edit: Ah, I see what you're trying to do, apply multiple filters to the same image. Try this:

 Bitmap image = ...;
 for(int i =0; i < someNumber; i++){
      Bitmap newImage = doSomeThing(image, width, height);
      image.recycle();
      image = newImage;
 }

Upvotes: 1

P_C
P_C

Reputation: 223

On a Java standpoint the System.gc() will probably have no effect because as you run out of memory Java will clear it if it can.

Your doSomeThing() call is probably creating too many object for the amount of memory you have.

Either increase it and if you can't because of constraints on memory footprint, adjust your Eden space.

Upvotes: 0

Related Questions