bulletphysics / bullet3

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
http://bulletphysics.org
Other
12.44k stars 2.86k forks source link

resetSimulation still does not free memory when using EGL #4149

Open akloss opened 2 years ago

akloss commented 2 years ago

Hi,

and sorry to open this up again, but it seems this is still not solved (or again not working). When using the egl renderer, I'm seeing an increase in GPU memory when loading URDF models and then removing them again and resetting the simulation. I'm on pybullet version 3.2.1, CUDA 11.2 and Ubuntu 18.04.

The code I'm using to test is below. Increases are relatively small for untextured objects but become larger if the urdf includes textures.

import matplotlib.pyplot as plt
import pybullet as p
import pkgutil
import pybullet_data
import time

client_id = p.connect(p.DIRECT)
egl = pkgutil.get_loader('eglRenderer')
p.loadPlugin(egl.get_filename(),
                    "_eglRendererPlugin",
                   physicsClientId=client_id)
p.setPhysicsEngineParameter(enableFileCaching=0)
p.setAdditionalSearchPath(pybullet_data.getDataPath())

urdf_paths = ['franka_panda/panda.urdf''] * 100

def render(urdf_path):
    body_id = p.loadURDF(urdf_path, globalScale=2)
    w, h, rgba, depth, mask = p.getCameraImage(
        width=320,
        height=240)
    return rgba, body_id

for ind, ob in enumerate(urdf_paths):
    try:
        im, body = render(ob)
        print(ind, ob)
        # fig, ax = plt.subplots()
        # ax.imshow(im)
        # plt.show()

        p.removeBody(body, client_id)
        p.resetSimulation()
    except Exception:
        pass

If p.resetSimulation() is commented out, the GPU memory will not increase, but rendering errors occur after a certain number of different URDFs are loaded (seems to be depend on their size): Then the objects are not rendered at all or their texture is not rendered. It doesn't throw any errors in both cases. If I load the URDFs without previously loading other files, they all work well. I couldn't actually reproduce this issue with the code above, but it's easily triggered with my own urdf files (which are much larger than the files in pybullet_data). Also, since it only happens if you load different URDFs, it seems like some caching is happening despite of enableFileCaching=0?

And finally something maybe unrelated: I noticed that the rendering of the franka arm looks different the second time it gets loaded, independent of whether p.resetSimulation() is called or not:

panda1 panda2

If you have any ideas how to fix the memory leak I'd be very grateful!

smnhdw commented 2 years ago

Hi, to solve your memory leak you should use p.saveState() and p.restoreState() instead. There's also an example provided https://github.com/bulletphysics/bullet3/blob/master/examples/pybullet/examples/saveRestoreState.py .