Android createGraphics memory problem

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
	at processing.a2d.PGraphicsAndroid2D.loadPixels(
	at processing.a2d.PGraphicsAndroid2D.endDraw(
	at processing.test.sketch_20180813a.sketch_20180813a.overlayScreen(
	at processing.test.sketch_20180813a.sketch_20180813a.setup(
	at processing.core.PApplet.handleDraw(
	at processing.core.PSurfaceNone.callDraw(
	at processing.core.PSurfaceNone$


PGraphics ops;
int counter;
long freeMemory;

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

void draw(){

void overlayScreen() {  
  ops = createGraphics(counter, 500);
  ops.line(0, 40, counter, 40);

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?


@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.

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