LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: jhovarie on April 10, 2018, 02:32:04

Title: Memory Leak from texture how to clean it
Post by: jhovarie 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.

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?
Title: Re: Memory Leak from texture how to clean it
Post by: KaiHH 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?)
Title: Re: Memory Leak from texture how to clean it
Post by: jhovarie on April 11, 2018, 01:14:31
YES my image is dynamic it needs to change every frames.
Title: Re: Memory Leak from texture how to clean it
Post by: Andrew Alfazy on April 11, 2018, 23:11:50
why when you unbind you use glDisable(GL_TEXTURE_2D); you disabled the Textures not unbind it you must call 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.
Title: Re: Memory Leak from texture how to clean it
Post by: jhovarie on April 12, 2018, 02:22:31
I am developing and video editing software... thats why I need to update the texture.
Title: Re: Memory Leak from texture how to clean it
Post by: darkyellow 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