Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OutOfMemory in image() #1391

Closed
processing-bugs opened this issue Feb 10, 2013 · 16 comments
Closed

OutOfMemory in image() #1391

processing-bugs opened this issue Feb 10, 2013 · 16 comments
Labels
core critical Highest possible priority for the next release imported

Comments

@processing-bugs
Copy link

Original author: fjen...@gmail.com (October 29, 2012 11:55:40)

I think the ImageCache introduced a memory leak.

Here's a simple example that already shows the problem:

PGraphics pg;

void setup ()
{
size( 200, 200 );

}

void draw ()
{
pg = createGraphics( width, height );
pg.beginDraw();
pg.background(random(255));
pg.endDraw();

image( pg, 0, 0 );

pg = null;

}

This runs up to around 3GB of (real) memory in less than a minute .. until it fails with OutOfMemory.

Removing the image() call removes the problem.

Os-X 10.6.8, MacBook
Tested on SVN (< rev 10366) and 2.0b5
Tested with OPENGL and JAVA2D

Original issue: http://code.google.com/p/processing/issues/detail?id=1353

@processing-bugs
Copy link
Author

From f...@processing.org on October 29, 2012 12:18:50
Hm, shouldn't be happening since the cache objects are connected by weak references. But maybe something isn't wired correctly there.

What happens if you set frameRate(10) or 20 inside setup()? I wonder if the (heavyweight) createGraphics() and image() calls aren't allowing any sleep time between draw() calls and therefore starving the GC.

@processing-bugs
Copy link
Author

From fjen...@gmail.com on October 29, 2012 13:36:57
No, neither frameRate(1) or calling System.gc() every nth frame helps. ... The choking would happen too when the image() call is removed but that cures the problem. So it needs to be somewhere in there ..

@processing-bugs
Copy link
Author

From f...@processing.org on October 29, 2012 13:40:31
k thanks; sounds like there's just a bug in how the weak references are implemented since they're not getting collected.

@processing-bugs
Copy link
Author

From nonsequi...@gmail.com on November 18, 2012 02:17:03
I think the same bug is causing big problems for me, but I thought loadImage was the culprit. Here's a simple sketch that causes memory use to climb rapidly. Lowering framerate or sleeping doesn't help. Same problem on Mac 64 and Win 64 if that helps.

String[] img_filenames;

void setup()
{
// filenames for two small images
// (mine are about 128 x 128)
img_filenames = new String[2];
img_filenames[0] = "/Users/dan/Documents/Owens Archive Crops/thumb/1857.1.jpg";
img_filenames[1] = "/Users/dan/Documents/Owens Archive Crops/thumb/1949.27.jpg";
frameRate(10); // tried different rates to give GC time, doesn't help
}

int i = 0; // image filename index

void draw()
{
PImage img = loadImage(img_filenames[i]); // load image
image(img, 0, 0); // display image
i = (i + 1) % img_filenames.length; // index of next image
}

@processing-bugs
Copy link
Author

From f...@processing.org on November 18, 2012 03:08:29
No, comment #5 is a programming error. You should never use loadImage() inside draw(), otherwise you're re-loading the image files (not a small amount of memory) 60 times every second. That's a bad idea.

@processing-bugs
Copy link
Author

From nonsequi...@gmail.com on November 18, 2012 13:25:51
My code example in comment #5 is artificial to illustrate a memory leak with loadImage() or PImage. Sure this toy example is poor programming style, but re-loading an image should not consume more memory, just more CPU -- whether in draw() or other places.

I have an app that runs all day in a gallery and essentially displays random images loaded from disk. There are thousands of possible images, so I have to load them on demand. I have to use loadImage() constantly and it's causing a memory leak.

@processing-bugs
Copy link
Author

From igno...@gmail.com on November 29, 2012 23:27:58
This seems to be the same issue I have encountered in assigning a supposedly temporary PImage variable in a method, see https://forum.processing.org/topic/pimage-memory-leak-example#25080000001807951.

PhiLho offered a more compact example than mine, with a workaround:

void draw()
{
  PImage img = createImage(width, height, RGB);   // leak
  image(img, 0, 0);
  g.removeCache(img);                             // workaround
  println(frameCount + " " + g.getCache(img));
}

The same problem occurs outside a tight loop, in my example. I've tracked it with JConsole. Garbage collection still happens in Processing 1.5.1 but not in Processing 2.0b6.

@processing-bugs
Copy link
Author

From goo...@superduper.org on December 16, 2012 22:19:47
i don't agree that #5 should be considered a programming error.
loading images depending on user interaction is a quite common thing (for me at least)

#8 actually fixes it for me.

only took a rough look, what strikes me is that there's no awtImage.flush() when the image is loaded
http://code.google.com/searchframe#Ej56LtI_pY0/trunk/processing/core/src/processing/core/PApplet.java&l=5144
(maybe my oversight)

@processing-bugs
Copy link
Author

From f...@processing.org on December 16, 2012 23:21:33
No, you're completely mischaracterizing what I said. I said re-loading images inside draw(), which by default runs at 60 frames per second, is a programming error. That's never going to work properly, and is really bad practice.

Loading on user interaction is of course important, but if you need new images at 60 fps, you want a movie, or for the images to be loaded once inside setup(), then swapped in as necessary.

But the general fact that it runs out of memory is a really significant bug that I haven't had time to track down yet.

@corporatefun
Copy link

Hi, I've been experiencing a similar problem with using PShapes that has only been occurring in Windows 7. Works fine in Mac:

https://forum.processing.org/topic/pshape-memory-leak-in-windows#25080000002001447

@codeanticode
Copy link
Member

Fixed in commit 3b64812

@P0ulp
Copy link

P0ulp commented Jun 5, 2013

Hello,

I always have this memory leak issue in the 2.09 version, my code call in draw() :

this.cam.loadPixels();
PImage temp = this.cam.get(int((this.cam.width-this.widthGlitchedImage)/2), 0, int(this.widthGlitchedImage), this.cam.height);
temp.resize(this.widthDetectImage, this.heightDetectImage);
this.opencv.copy(temp);
g.removeCache(temp); // Out of memory withou this
this.faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 20, 20);

Is-it relay fixed ?

Thanks

@corporatefun
Copy link

Yes, I also used version 2.0b9 and still have memory leaks with setupFrameResizeListener()

#1857

@P0ulp
Copy link

P0ulp commented Jun 5, 2013

But this issue seems to be fixed in the 2.0 stable verions ! :)

@benfry
Copy link
Contributor

benfry commented Jun 20, 2013

The issue is fixed, please use the forum for further discussion.

jarkW added a commit to jarkW/eleven-QA that referenced this issue Jun 26, 2016
When loading certain streets e.g. Chavila Checker, the programme
exceeded 256 MB, even when just one snap loaded and only about 7 quoins.
Removing the cache for the image when it was unloaded appeared to fix
the problem
(processing/processing#1391).
There is still some memory problem which I think is down to Processing
as a simple load street snap/unload street snap showed that after the
unload, the memory didn't return to the initial level before the first
load - some memory is not being released. Not sure I can do anything
about that other than set the memory bar for the programme a bit higher
e.g. to 300mb.

As it is useful, on level 1 error tracing, memory information is dumped
out when loading/unloading images - can then be put in a spreadsheet
(import, split at ,) and graphed so can see what is happening.

Also changed quoin output info to only give the class name - type is
irrelevant as included in class name information. And improved some
tracing for when start processing an item on a street.
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
core critical Highest possible priority for the next release imported
Projects
None yet
Development

No branches or pull requests

5 participants