Android createGraphics memory problem

Hi.
Using code below it sometimes gives, and sometimes not, an error: (counter value below 4000 does not give an error. -using APDE-)

Free memory = 4649576
java.lang.OutOfMemoryError
	at processing.a2d.PGraphicsAndroid2D.loadPixels(PGraphicsAndroid2D.java:2024)
	at processing.a2d.PGraphicsAndroid2D.endDraw(PGraphicsAndroid2D.java:262)
	at processing.test.sketch_20180813a.sketch_20180813a.overlayScreen(sketch_20180813a.java:43)
	at processing.test.sketch_20180813a.sketch_20180813a.setup(sketch_20180813a.java:28)
	at processing.core.PApplet.handleDraw(PApplet.java:1801)
	at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)

Code:

PGraphics ops;
int counter;
long freeMemory;

void setup(){
  fullScreen();
  counter = 10000;
  freeMemory = Runtime.getRuntime().freeMemory();
  println("Free memory = " + freeMemory);
  overlayScreen();
  image(ops, -9900, 0);
}

void draw(){
}

void overlayScreen() {  
  ops = createGraphics(counter, 500);
  ops.beginDraw();
  ops.stroke(50);
  ops.line(0, 40, counter, 40);
  ops.endDraw();
}

Any idea how to prevent?

Obligatory “I’m not an expert” but I’'m under the impression that, phones, being phones, do not have as much memory to work with as PC’s.

A little googling brought up this issue on the android-processing giuthub:

Given this, (and I’m just tossing ideas), consider removeCache()?

Also, check out this thread from 2013:

1 Like

Maybe instead of 10000, try a smaller value?

What are you trying to do by the way?

Kf

@tony and @kfrajer Thanks for the answer.
Indeed using removeCache() along with System.gc(); and setting (quite a lot of) images to null
solved the problem.
Thanks.

I apologize, now I am also looking for a solution to this problem.
Can you explain how to do “using removeCache() along with System.gc(); and setting (quite a lot of) images to null”? You are welcome)))