-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix memory leak in ManagerBasedEnv.close #5925
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
Open
cvolkcvolk
wants to merge
2
commits into
isaac-sim:develop
Choose a base branch
from
cvolkcvolk:cvolk/fix-manager-based-env-close-leaks
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
source/isaaclab/test/envs/test_manager_based_env_close_leak.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). | ||
| # All rights reserved. | ||
| # | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| """Regression test for the ``ManagerBasedEnv.close`` memory leak fixed in this PR.""" | ||
|
|
||
| """Launch Isaac Sim Simulator first.""" | ||
|
|
||
| from isaaclab.app import AppLauncher | ||
|
|
||
| simulation_app = AppLauncher(headless=True).app | ||
|
|
||
| """Rest everything follows.""" | ||
|
|
||
| import gymnasium as gym | ||
| import pytest | ||
| import torch | ||
|
|
||
| import isaaclab_tasks # noqa: F401 -- registers Isaac-* env IDs with gymnasium | ||
| from isaaclab_tasks.utils.parse_cfg import parse_env_cfg | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device", ["cuda:0", "cpu"]) | ||
| def test_close_clears_obs_buf_and_releases_spaces(device): | ||
| """``ManagerBasedEnv.close`` should release the cached observation buffer and the | ||
| gym space attributes attached to the env. | ||
|
|
||
| Without the release, gymnasium's wrapper chain (e.g. ``OrderEnforcing`` plus the | ||
| ``make()`` registry) keeps the env reachable past ``close``, so ``self.obs_buf`` | ||
| (the dict of cached observation tensors — tens of MB per image sensor) and the | ||
| ``observation_space`` / ``action_space`` ``gym.spaces.Box`` attributes (~110 MB | ||
| of numpy bounds-array memory per image-observation Box at ``(num_envs, H, W, C)`` | ||
| float32) survive the call and accumulate on each construct/teardown cycle. | ||
| """ | ||
| env_cfg = parse_env_cfg("Isaac-Cartpole-v0", device=device, num_envs=2) | ||
| env = gym.make("Isaac-Cartpole-v0", cfg=env_cfg) | ||
| env.reset() | ||
| # Step once so the ObservationManager populates ``obs_buf``. | ||
| action = torch.zeros((2, env.action_space.shape[-1]), device=env.unwrapped.device) | ||
| env.step(action) | ||
| assert env.unwrapped.obs_buf, "precondition: obs_buf should be populated after step" | ||
| assert env.unwrapped.observation_space is not None, "precondition: observation_space should be set" | ||
| assert env.unwrapped.action_space is not None, "precondition: action_space should be set" | ||
|
|
||
| env.close() | ||
|
|
||
| assert not env.unwrapped.obs_buf, "obs_buf was not cleared on close" | ||
| assert env.unwrapped.observation_space is None, "observation_space was not released on close" | ||
| assert env.unwrapped.action_space is None, "action_space was not released on close" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
single_observation_space/single_action_spaceThe fix in
ManagerBasedRLEnv.close()nulls all four space attributes (single_observation_space,single_action_space,observation_space,action_space), but the test only asserts the last two. Thesingle_*variants are the per-environmentgym.spaces.Boxobjects described in the PR description as holding ~110 MB of bounds arrays for image observations — they are exactly the attributes whose memory the fix aims to release. Leaving them unchecked means a regression that stops nullingsingle_observation_spacewould go undetected.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!