stable-baselines3 icon indicating copy to clipboard operation
stable-baselines3 copied to clipboard

[Bug]: VecVideoRecorder RAM USAGE

Open vicbentuupc opened this issue 7 months ago • 1 comments

🐛 Bug

I was training and used VecVideoRecorder to save some episodes. I saw that RAM usage kept increasing until crashing. I tried testing it to see what's happening with the following code:

from stable_baselines3.common.vec_env import SubprocVecEnv
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.vec_env.vec_video_recorder import VecVideoRecorder

import gymnasium as gym
import numpy as np


def make_env(i):
    def _init():
        env = gym.make("LunarLander-v3", render_mode="rgb_array")
        env = Monitor(env)
        return env
    return _init

if __name__ == "__main__":
    n_envs = 1
    env = SubprocVecEnv([make_env(i) for i in range(n_envs)])
    env = VecVideoRecorder(
        env,
        r"videos",
        record_video_trigger=lambda x: True,
    )


    num_episodes_to_watch = 50
    lstm_state = None
    episode_start = np.ones((n_envs,), dtype=bool)

    obs = env.reset()
    num_episodes = [0] * n_envs


    from datetime import datetime
    startTime = datetime.now()
    counter = 0

    while any(ep < num_episodes_to_watch for ep in num_episodes):

        counter += 1
        if counter % 100 == 0:
            print(f"{counter} frames have passed, FPS: {counter/(datetime.now()-startTime).total_seconds()}")


        actions = [env.action_space.sample() for _ in range(n_envs)]


        next_obs, rewards, dones, infos = env.step(actions)


        episode_start = dones.copy()
        for idx, done in enumerate(dones):
            if done:
                num_episodes[idx] += 1
        obs = next_obs

    env.close()

Am I doing something wrong? I read and think maybe I should be closing the env and opening it again, but I want to use this on a custom env that is pretty heavy and I don't want to be oppening it and closing it constantly.

To Reproduce

from stable_baselines3 import ...

Relevant log output / Error message


System Info

No response

Checklist

  • [x] My issue does not relate to a custom gym environment. (Use the custom gym env template instead)
  • [x] I have checked that there is no similar issue in the repo
  • [x] I have read the documentation
  • [x] I have provided a minimal and working example to reproduce the bug
  • [x] I've used the markdown code blocks for both code and stack traces.

vicbentuupc avatar May 10 '25 13:05 vicbentuupc

Hello, if you add gc.collect() after num_episodes[idx] += 1, this should force garbage collection and solve your problem (tested locally).

araffin avatar May 20 '25 19:05 araffin