Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory usage keeps increasing while rerunning the simulation multiple times #527

Closed
iicfcii opened this issue Oct 12, 2022 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@iicfcii
Copy link

iicfcii commented Oct 12, 2022

I am a PhD student trying to use mujoco to simulate the control of a robot for reinforcement learning. I am reloading the xml string and recreating the model for every simulation run to adjust certain parameters such as body mass, joint stiffness, or damping. If the learning process is long, the code will gradually eat up all the available memory.

Here is a sample code to demonstrate the problem I ran into. If you ran the code, the memory usage (reported in the task manager) of the python process keeps increasing. An interesting observation is that if I commented out mujoco.set_mjcb_control(cb) or move callback function directly before mujoco.mj_step(model, data), the memory usage becomes stable.

What is the proper way to rerun the simulation within a python process? Or is this actually a bug?

Environment:
Windows 10
python 3.9
mujoco 2.2.2

import mujoco
import time

XML = r"""
<mujoco>
  <worldbody>
    <geom type="box" size="1 1 0.1"/>
    <body>
      <freejoint/>
      <geom type="box" size="0.1 0.1 0.1" pos="0 0 1"/>
    </body>
  </worldbody>a
</mujoco>
"""


def cb(m, d):
    pass


for i in range(100):
    mujoco.set_mjcb_control(None)
    model = mujoco.MjModel.from_xml_string(XML, {})
    data = mujoco.MjData(model)
    mujoco.set_mjcb_control(cb)
    while data.time < 10:
        mujoco.mj_step(model, data)
    time.sleep(1)
@iicfcii iicfcii added the question Request for help or information label Oct 12, 2022
@iicfcii iicfcii changed the title Memory usage keeps increasing while res Memory usage keeps increasing while resetting and running the simulation multiple times Oct 12, 2022
@iicfcii iicfcii changed the title Memory usage keeps increasing while resetting and running the simulation multiple times Memory usage keeps increasing while rerunning the simulation multiple times Oct 12, 2022
@yuvaltassa
Copy link
Collaborator

Thanks.
Can you please try calling gc.collect() in the loop and see if that affects the memory growth?

@iicfcii
Copy link
Author

iicfcii commented Oct 12, 2022

I added gc.collect() in the loop but the memory is still growing.

@saran-t
Copy link
Member

saran-t commented Oct 14, 2022

I'm able to repro the memory footprint increase, however if I add gc.collect() immediately after the inner loop (i.e. where you currently have time.sleep(1) now) the memory usage no longer increases.

Could you please confirm that this is what you see as well?

Nvm, my bad. I can repro even with gc.collect().

@saran-t
Copy link
Member

saran-t commented Oct 14, 2022

Fix incoming.

@saran-t saran-t added bug Something isn't working and removed question Request for help or information labels Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants