This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

glTexImage2D memory leak

Hello Everyone! I have a question about function glTexImage2D.

There is some code that cause GL memory leak on the devices with mali gpu (t760) and force close android application.

  ....

    struct Vertex

    {

         float x, y, z;

         float u, v;

    };

  ...

    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    glViewport(0, 0, 1440, 2560);

    while(true)

    {      

        glActiveTexture(GL_TEXTURE0);

        glBindTexture(GL_TEXTURE_2D, texID);

       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

   

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1440, 2560, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

   

        glUseProgram(shader);

        int position = glGetAttribLocation(shader, "aPosition");

        glVertexAttribPointer(hPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &vertexBuffer[0]);

        glEnableVertexAttribArray(position);

   

        int texCoord = glGetAttribLocation(data.shader, "aTexCoord");

        glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &vertexBuffer[0].u);

        glEnableVertexAttribArray(texCoord);

        int textureLocation = glGetUniformLocation(shader, "texture0");

        glUniform1i(textureLocation, 0);

        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

        glDisableVertexAttribArray(position);

        glDisableVertexAttribArray(texCoord);

    }

The shaders program is a trivial, just gets texture sampler and draws at the target FBO.

This code is just rendering texture into FBO. On each steps texture is updated by glTexImage2D call.

The question is why this code leads to memory leak?

I suppose, the reason is that glTexImage2D on each call allocates new chunk of memory for texture's data.

Texture memory that are used at previous rendering step is never released, until we call glFinish or glBindFramebuffer(GL_FRAMEBUFFER, 0).

It looks like some optimization on driver side, but is it standard behavior? Please, correct me if I'm wrong.

Thanks a lot!

P.S Code above works fine on the devices with Adreno GPU.

Parents
  • So, thanks guys for yours answers. It helps me understand how it works under hood.

    All workaround was mentioned before (I think, the best one is periodically flush (for example, calls glFinish))

    I believe, sometime we can see resolved this issue on the driver side

    Kind regards,

    Igor

Reply
  • So, thanks guys for yours answers. It helps me understand how it works under hood.

    All workaround was mentioned before (I think, the best one is periodically flush (for example, calls glFinish))

    I believe, sometime we can see resolved this issue on the driver side

    Kind regards,

    Igor

Children
  • Some general observations for "real app" behavior.

    1. Uploading 15MB of texture data (2560x1440x4)  for every draw call is going to be really really slow - because of memory allocation and copy overheads if nothing else - and if you have lots of drawcalls you are going to thrash the memory system and texture caches quite badly because your textures are large and uncompressed. It seems like a very strange usage model - what are you actually trying to do?
    2. Note that periodically draining the pipeline via glFlush or glFinish will have a performance impact (we will have to flush out the intermediate state to memory).

    HTH,
    Pete