| |
| |
| |
| |
|
|
| """ |
| This script demonstrates reference count of the robot view in Isaac Sim. |
| |
| When we make a class instance, the reference count of the class instance should always be 1. |
| However, in this script, the reference count of the robot view is 2 after the class is created. |
| This causes a memory leak in the Isaac Sim simulator and the robot view is not garbage collected. |
| |
| The issue is observed with torch 2.2 and Isaac Sim 4.0. It works fine with torch 2.0.1 and Isaac Sim 2023.1. |
| It can be resolved by uncommenting the line that creates a dummy tensor in the main function. |
| |
| To reproduce the issue, run this script and check the reference count of the robot view. |
| |
| For more details, please check: https://github.com/isaac-sim/IsaacLab/issues/639 |
| """ |
|
|
| """Launch Isaac Sim Simulator first.""" |
|
|
|
|
| import contextlib |
|
|
| with contextlib.suppress(ModuleNotFoundError): |
| import isaacsim |
|
|
| from isaacsim import SimulationApp |
|
|
| |
| simulation_app = SimulationApp({"headless": True}) |
|
|
| """Rest everything follows.""" |
|
|
| import ctypes |
| import gc |
| import logging |
|
|
| import torch |
|
|
| import isaacsim.core.utils.nucleus as nucleus_utils |
| import isaacsim.core.utils.prims as prim_utils |
| from isaacsim.core.api.simulation_context import SimulationContext |
| from isaacsim.core.prims import Articulation |
|
|
| |
| logger = logging.getLogger(__name__) |
|
|
|
|
| |
| if nucleus_utils.get_assets_root_path() is None: |
| msg = ( |
| "Unable to perform Nucleus login on Omniverse. Assets root path is not set.\n" |
| "\tPlease check: https://docs.omniverse.nvidia.com/app_isaacsim/app_isaacsim/overview.html#omniverse-nucleus" |
| ) |
| logger.error(msg) |
| raise RuntimeError(msg) |
|
|
|
|
| ISAAC_NUCLEUS_DIR = f"{nucleus_utils.get_assets_root_path()}/Isaac" |
| """Path to the `Isaac` directory on the NVIDIA Nucleus Server.""" |
|
|
| ISAACLAB_NUCLEUS_DIR = f"{ISAAC_NUCLEUS_DIR}/IsaacLab" |
| """Path to the `Isaac/IsaacLab` directory on the NVIDIA Nucleus Server.""" |
|
|
|
|
| """ |
| Classes |
| """ |
|
|
|
|
| class AnymalArticulation: |
| """Anymal articulation class.""" |
|
|
| def __init__(self): |
| """Initialize the Anymal articulation class.""" |
| |
| usd_path = f"{ISAACLAB_NUCLEUS_DIR}/Robots/ANYbotics/ANYmal-C/anymal_c.usd" |
| |
| print("Loading robot from: ", usd_path) |
| prim_utils.create_prim("/World/Robot", usd_path=usd_path, translation=(0.0, 0.0, 0.6)) |
|
|
| |
| root_prim_path = "/World/Robot/base" |
| |
| self.view = Articulation(root_prim_path, name="ANYMAL") |
|
|
| def __del__(self): |
| """Delete the Anymal articulation class.""" |
| print("Deleting the Anymal view.") |
| self.view = None |
|
|
| def initialize(self): |
| """Initialize the Anymal view.""" |
| self.view.initialize() |
|
|
|
|
| """ |
| Main |
| """ |
|
|
|
|
| def main(): |
| """Spawns the ANYmal robot and clones it using Isaac Sim Cloner API.""" |
|
|
| |
| sim = SimulationContext(physics_dt=0.005, rendering_dt=0.005, backend="torch", device="cuda:0") |
|
|
| |
| |
| sim._settings.set_bool("/persistent/omnihydra/useSceneGraphInstancing", True) |
|
|
| |
| |
| |
|
|
| |
| robot = AnymalArticulation() |
|
|
| print("Reference count of the robot view: ", ctypes.c_long.from_address(id(robot)).value) |
| print("Referrers of the robot view: ", gc.get_referrers(robot)) |
| print("---" * 10) |
|
|
| |
| sim.reset() |
|
|
| print("Reference count of the robot view: ", ctypes.c_long.from_address(id(robot)).value) |
| print("Referrers of the robot view: ", gc.get_referrers(robot)) |
| print("---" * 10) |
|
|
| robot.initialize() |
|
|
| print("Reference count of the robot view: ", ctypes.c_long.from_address(id(robot)).value) |
| print("Referrers of the robot view: ", gc.get_referrers(robot)) |
| print("---" * 10) |
|
|
| |
| sim.stop() |
|
|
| print("Reference count of the robot view: ", ctypes.c_long.from_address(id(robot)).value) |
| print("Referrers of the robot view: ", gc.get_referrers(robot)) |
| print("---" * 10) |
|
|
| |
| sim.clear() |
|
|
| print("Reference count of the robot view: ", ctypes.c_long.from_address(id(robot)).value) |
| print("Referrers of the robot view: ", gc.get_referrers(robot)) |
| print("---" * 10) |
|
|
|
|
| if __name__ == "__main__": |
| |
| main() |
| |
| simulation_app.close() |
|
|