Hello Guest

Memory Leak from texture how to clean it

  • 5 Replies
  • 5313 Views
Memory Leak from texture how to clean it
« on: April 10, 2018, 02:32:04 »
Hello I need help. I need to to set the texture inside the loops to update the display. but it cause memory leaks. my codes looks like this.

Code: [Select]
public void setTexture(BufferedImage bi) {
try {
width = bi.getWidth();
height = bi.getHeight();

int[] pixels_raw = new int[width * height ];
pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
int counterIndex=0;
ByteBuffer pixels = BufferUtils.createByteBuffer(width * height *4);
for(int i = 0; i < width; i++) {
          for(int j = 0; j < height; j++) {
                 int pixel = pixels_raw[counterIndex];
                 pixels.put((byte)((pixel >> 16) & 0xFF));//RED
                 pixels.put((byte)((pixel >> 8) & 0xFF));//GREEN
                 pixels.put((byte)(pixel & 0xFF));       //BLUE
                 pixels.put((byte)((pixel >> 24) & 0xFF)); //Alpha
                 counterIndex++;
         }
}
pixels.flip();

id = glGenTextures();

glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,height, 0, GL_RGBA, GL_UNSIGNED_BYTE,pixels);
}catch(Exception e) {
e.printStackTrace();
}
}

public void bind(int sampler) {
if(sampler >= 0 && sampler <= 31) {
glActiveTexture(GL_TEXTURE0 + sampler);
glBindTexture(GL_TEXTURE_2D, id);
}
}

public void unbind(int sampler) {
if(sampler >= 0 && sampler <= 31) {
glActiveTexture(GL_TEXTURE0 + sampler);
glDisable(GL_TEXTURE_2D);
}
}


//I need to set the texture inside the loops but it cause memory leak how to fixed this?
while(true){
..
tex.setTexture(mybufferedimage);
tex.bind(0);
tex.unbind(0);
..
}
   
   Any help?

*

Offline KaiHH

  • ****
  • 334
Re: Memory Leak from texture how to clean it
« Reply #1 on: April 10, 2018, 07:08:40 »
You are generating tons and tons and tons of textures with generating a new free texture name/handle with glGenTextures(), binding that texture with glBindTexture() and allocating storage and filling it with texels with glTexImage2D().
And apparently you do this every frame.

Please note: OpenGL does not work together with Java's garbage collector, so you must make sure to glDeleteTextures() the texture once you do not need it anymore. But an even better idea would be to REUSE a single texture and not constantly create and delete new ones.
Also: Do you really need to use a BufferedImage and on top of that constantly pump the texels of that BufferedImage into a newly crearted ByteBuffer and then push that ByteBuffer into the OpenGL texture? Is your image really dynamic (i.e. does it change once per frame?)

Re: Memory Leak from texture how to clean it
« Reply #2 on: April 11, 2018, 01:14:31 »
YES my image is dynamic it needs to change every frames.

*

Andrew Alfazy

Re: Memory Leak from texture how to clean it
« Reply #3 on: April 11, 2018, 23:11:50 »
why when you unbind you use
Code: [Select]
glDisable(GL_TEXTURE_2D); you disabled the Textures not unbind it you must call
Code: [Select]
glBindTexture(GL_TEXTURE_2D, 0);and sorry for this question but What type of project you do that require you to reload and read new Image every frame ?!! that's performance killing.

Re: Memory Leak from texture how to clean it
« Reply #4 on: April 12, 2018, 02:22:31 »
I am developing and video editing software... thats why I need to update the texture.

Re: Memory Leak from texture how to clean it
« Reply #5 on: April 12, 2018, 20:58:06 »
Use Pixel Buffer Objects, create two of them for double buffering. You now only need one texture and can stream video using opengl. This is the method I use.

The concept is mentioned here : http://www.songho.ca/opengl/gl_pbo.html