Principle:Google deepmind Mujoco Resource Cleanup
| Knowledge Sources | |
|---|---|
| Domains | Memory_Management, Physics_Simulation |
| Last Updated | 2026-02-15 06:00 GMT |
Overview
Pattern for properly deallocating MuJoCo simulation resources to prevent memory leaks.
Description
Resource Cleanup ensures that all dynamically allocated MuJoCo structures are properly freed. MuJoCo uses C-style manual memory management, so every mj_makeData must be paired with mj_deleteData, and every model load must be paired with mj_deleteModel. Data must be freed before its associated model, as the model contains size information used during deallocation.
Usage
Apply this principle at simulation shutdown or when reinitializing. Always free data before model. In C++ applications, consider RAII wrappers or unique_ptr with custom deleters.
Theoretical Basis
The cleanup order follows the inverse of the allocation order:
# Abstract cleanup sequence (not real code)
free_visualization_resources() # scene, context
free_simulation_data(d) # mjData
free_model(m) # mjModel