# Android createGraphics memory problem

**URL:** <https://discourse.processing.org/t/android-creategraphics-memory-problem/2916>\
**Category:** Processing for Android\
**Created:** [August 24, 2018, 10:40am UTC](https://discourse.processing.org/t/android-creategraphics-memory-problem/2916 "2018-08-24T10:40:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 24, 2018, 10:40am UTC](https://discourse.processing.org/t/android-creategraphics-memory-problem/2916/1 "2018-08-24T10:40:33Z")

</div>

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

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 24, 2018, 6:28pm UTC](https://discourse.processing.org/t/android-creategraphics-memory-problem/2916/2 "2018-08-24T18:28:04Z")

</div>

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:

> <https://github.com/processing/processing-android/issues/469>

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

Also, check out this thread from 2013:

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/1351/outofmemoryerror-on-android)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 24, 2018, 9:09pm UTC](https://discourse.processing.org/t/android-creategraphics-memory-problem/2916/3 "2018-08-24T21:09:24Z")

</div>

Maybe instead of 10000, try a smaller value?

What are you trying to do by the way?

Kf

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 26, 2018, 9:16pm UTC](https://discourse.processing.org/t/android-creategraphics-memory-problem/2916/4 "2018-08-26T21:16:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NoName](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@NoName](https://discourse.processing.org/u/NoName)\
**Post date:** [July 11, 2019, 1:55pm UTC](https://discourse.processing.org/t/android-creategraphics-memory-problem/2916/5 "2019-07-11T13:55:48Z")

</div>

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