We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › OpenGL bug Memory leak
Page Index Toggle Pages: 1
OpenGL bug? Memory leak? (Read 825 times)
OpenGL bug? Memory leak?
Mar 26th, 2008, 11:01pm
 
Ok. I made some measurements of memory needs for image displaying. And I'm quite astonished that it needs so much RAM to display few images.

The smallest program I could think of with the OpenGL library uses 39 MB. Here's the code:

Code:

import processing.opengl.*;

PImage i1,i2,i3,i4,i5,i6,i7,i8,i9;
int i=1;

void setup(){
size(1024,768,OPENGL);
hint(ENABLE_OPENGL_2X_SMOOTH);
background(0);

void draw(){
}




If I only *load* (no displaying yet) 7 images (1.53 MB for all 7 files) this sample sketch uses 82MB according to windows task manager.

Code:

import processing.opengl.*;

PImage i1,i2,i3,i4,i5,i6,i7,i8,i9;
int i=1;

void setup(){
size(1024,768,OPENGL);
hint(ENABLE_OPENGL_2X_SMOOTH);
background(0);
i1=loadImage("pr/1.png");
i2=loadImage("pr/2.png");
i3=loadImage("pr/3.png");
i4=loadImage("pr/4.png");
i5=loadImage("pr/5.png");
i6=loadImage("pr/6.png");
i7=loadImage("pr/7.png");
}

void draw(){
}


Then if I display:

image 1 - 114
image 2 - 122
image 3 - 140
image 4 - 165
image 5 - 156
image 6 - 185
image 7 - 225

The values vary a bit, but not by much, maybe +/- 5MB.
Here's the code:

Code:

import processing.opengl.*;

PImage i1,i2,i3,i4,i5,i6,i7,i8,i9;
int i=1;

void setup(){
size(1024,768,OPENGL);
hint(ENABLE_OPENGL_2X_SMOOTH);
background(0);
i1=loadImage("pr/1.png");
i2=loadImage("pr/2.png");
i3=loadImage("pr/3.png");
i4=loadImage("pr/4.png");
i5=loadImage("pr/5.png");
i6=loadImage("pr/6.png");
i7=loadImage("pr/7.png");
}

void draw(){
}

void mouseClicked(){
switch(i){
case 1: image(i1,0,0,1024,768);
i++;
break;
case 2: image(i2,0,0,1024,768);
i++;
break;
case 3: image(i3,0,0,1024,768);
i++;
break;
case 4: image(i4,0,0,1024,768);
i++;
break;
case 5: image(i5,0,0,1024,768);
i++;
break;
case 6: image(i6,0,0,1024,768);
i++;
break;
case 7: image(i7,0,0,1024,768);
i++;
break;
}
}



How can this be so inefficient? I mean, using over 200 MB of RAM just to load 7 images while displaying only 1?

Why does the RAM keep adding up even when my images are already loaded and I'm just displaying a different one each time? Maybe an OpenGL bug?


**********EDIT********
I even tried using a jpg image instead of PNG with the same results.

Also, it's not only doing it in my developing computer, but in others as well. :/

JohnG, I know you can shed some light on this Smiley
Re: OpenGL bug? Memory leak?
Reply #1 - Mar 26th, 2008, 11:39pm
 
opengl will load images to graphics card memory and your images size in the harddrive does not always correspond to its correct size, basically because of file format compressions like png and jpg do.

if you loading a RGBA texture of say, its size would be: width*height*4. thats the size in bytes in memory depending on your image dimensions.

a 256x256x32bit texture would take 262144 bytes of memory allocation.

the RAM would be something else about java and other windows needed stuff to run your application. does not mean its your textures

also windows task manager shouldnt be 100% trusted.
Re: OpenGL bug? Memory leak?
Reply #2 - Mar 26th, 2008, 11:41pm
 
Wouldn't it convert the images to an uncompressed bitmap format after loading them? I know that libraries such as SDL do, but I'm not sure how a PImage is constructed

EDIT: crmx got there first and explained it better Smiley
Re: OpenGL bug? Memory leak?
Reply #3 - Mar 26th, 2008, 11:56pm
 
I think when using OpenGL all bets are off to some extent.

Having had a quick shufty through the source...

Processing stores the image as 4bytes/pixel. Then if you want to use it in OpenGL there has to be a GL-understood verison, which is the same size again. Then this has to be put into an IntBuffer for OpenGL to actually access which is at least the same size. And then OpenGL itself might make a copy of the data which would lead to a 4th copy. Oh, and then there's the mip-mapped versions, so add another 3/4 of a copy or so.

(For anyone looking at the source, that's the PImage.pixels[] PImage.cache.tpixels[] and PImage.cache.tbuffer for the 3 uncompressed definites)

So there's at least 3 copies of the pixel data for any image, and they're all uncompressed, possibly as many as 4 3/4 copies of the pixel data.

So for a nice big texture, say 1024x1204, you're looking at least 12MB, possibly as much as 20MB.
Re: OpenGL bug? Memory leak?
Reply #4 - Mar 27th, 2008, 12:01am
 
Woah, that's crazy. Good job I've got 2GB of RAM!
Re: OpenGL bug? Memory leak?
Reply #5 - Mar 27th, 2008, 12:43am
 
Thanks all for your answers. The thing is, I'm not even using the images for textures. I use OGL to draw lines in 3D, very simple stuff, and the images are just for info and menus.

For example:

http://saganus00.googlepages.com/projector.png
http://saganus00.googlepages.com/touchscreen.png

I display the first one from (0,0) to (1024,768) and the other one from (1024,0) to (2048,768). Thus, they are next to each other, using the vidcard to split them into two monitors (projector and touchscreen).

So, like I said, I'm not even doing anything to the images. No transformations, no nothing. Just using them like decals (decals is the correct term)

Also, I know task manager is not to be trusted, but it displays a fairly good consistency. Like, every single time I start the sketch from the above post, it shows the same memory usage (give or take some MB's). Click once the mouse and it will show more memory usage, but still around the same values each time, so it must not be *that* far from the actual usage. Even if it is completely wrong, at some point the program just crashes because of OutOfMemory error and I'm setting the max heap space to 1024 (max memory available in the production computer)

*********EDIT**********

BTW, my "analysis" technique is just to open the task manager in a visible place, then run the program and note the value. Click once to display first image, note the value. Click again to display image 2, note the value, etc.
Re: OpenGL bug? Memory leak?
Reply #6 - Mar 27th, 2008, 12:53am
 
Yeah, I think that because you're using the OpenGL renderer, the image function would have to adapt and display the image on a quad or something. I'm not sure on the inner workings of Processing, but I'd imagine that it doesn't use the same technique to render images in 3D as it does in 2D due to using a different render engine.

I could be wrong, anyone care to correct me?
Re: OpenGL bug? Memory leak?
Reply #7 - Mar 28th, 2008, 3:07am
 
Could fry or reas comment on this?

I'm pretty sure they must be occupied/on vacation/out of town or something, but when you guyys come back, can you post the specifics of memoryt usage or something?

Yey. Thanks.
Page Index Toggle Pages: 1