简体   繁体   中英

How to free memory allocated by LWJGL

My Java application with LWJGL allocates memory indefinitely until the application is dropped. Apparently this part of the code is causing the allocation of memory in looping. Is there a memory release missing? Tks for help.

public void renderMesh(GameObject object, Camera camera) {
    GL30.glBindVertexArray(object.getMesh().getVAO());
    GL30.glEnableVertexAttribArray(0);
    GL30.glEnableVertexAttribArray(1);
    GL30.glEnableVertexAttribArray(2);
    
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, object.getMesh().getIBO());
    
    shader.bind();
    shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotate(), object.getScale()));
    shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
    shader.setUniform("projection", window.getProjectionMatrix());
    
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMesh().getMaterial(0).getTextureID());

    GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
    
    shader.unbind();
    
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        
    GL30.glDisableVertexAttribArray(0);
    GL30.glDisableVertexAttribArray(1);
    GL30.glDisableVertexAttribArray(2);
    GL30.glBindVertexArray(0);
}

In your code there is nothing explicitly freeable. But there are possibly two elements:

  1. Are you sure to free resources and buffers used by meshes of your object?
  2. And did you delete your shader program after use?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM