# Memory Leak when training PPO on a single agent environment

**URL:** https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712
**Category:** RLlib
**Created:** [December 20, 2022, 4:05am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712 "2022-12-20T04:05:27Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 20, 2022, 4:05am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/1 "2022-12-20T04:05:28Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- High: It blocks me to complete my task.

I have actually been stuck on this memory leak for a while. I was originally using ray tune to setup and run my simple experiment. After hundreds of thousands of training iterations, the program would error out with an error telling me I ran out of memory. I tried configuring the program to use different number of workers, configuring the program to work with and without my gpu, and testing the environment itself for memory leaks ( external to ray ).

Because I couldn’t prevent the program from running out of space and I wasn’t at the point where I wanted to tune my algorithm, I changed my program to configure the algorithm directly with PPOConfig. I configured my PPO algorithm to use the MemoryTrackingCallback. I started off with testing only tens of iterations with a gpu and 6 workers. The memory leak persisted, so I tried using only a single worker without a gpu. I noticed that when using a single worker that I wasn’t running out of space on a short test of a couple hundred iterations. However, after that test was successful, I tried 2 workers with no gpu and the RAM usage percentage started to click up pretty quickly.

I went through the data from the custom metrics of the MemoryTrackingCallback and here are some graphs that show which things were ticking up with the RAM usage percentage.

![ram_util_percent](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/8/8d438331d3f3a4303efcedc8d2c2d8ec8f3a46aa.png)  
 ![tracemalloc_worker_data_mean](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/d/dad466032908b67df57067d353e07514e2324446.png)  
 ![tracemalloc_worker_rss_mean](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/1/13ab3c4d47cdff890cb8af857994af492559555b.png)  
 ![tracemalloc_worker_vms_mean](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/6/6956ba292a79605db3fc0c3f8a05f86a77729868.png)

I am not really sure what to do next. I have been stuck with this memory leak for a while now. I have read multiple threads here and on stackoverflow. I have read through the documentation and the source code. I’m stuck.

I think it is important to note that I am running this training program in a docker container as I did read somewhere that linux cgroups could be causing this problem.

I SHOULD ALSO NOTE THAT I AM USING python3.7 and ray[rllib]==2.0.1.

---

<div class="post-metadata">

### Author: ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)
#### Post date: [December 20, 2022, 12:35pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/2 "2022-12-20T12:35:38Z")

</div>

Hi @MrDracoG,

Here is a callback I use to help me track down memory leaks. I wrote and used this a while ago so you may need to update the method signature if it has changed.

You can tune how many objects to report on by changing the 50 to a suitable number.

```auto
sorted_object_count[:50]

```

```auto
 import gc
 
 class PythonObjectTrackingCallbacks(DefaultCallbacks):
     def __init__ (self):
         super(). __init__ ()
 
     def on_episode_end(
             self,
             *,
             worker,
             base_env,
             policies,
             episode,
             env_index=None,
             **kwargs):
         object_count = defaultdict(
             int)
         for obj in gc.get_objects():
             object_count[str(type(obj))] += 1
 
         sorted_object_count = sorted(object_count.items(), key=lambda item: item[1], reverse=True)
         for stat in sorted_object_count[:50]:
             obj_type = stat[0]
             obj_count = stat[1]
             episode.custom_metrics[f"gcobjects/{obj_type}/count"] = obj_count

```

---

<div class="post-metadata">

### Author: ![arturn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/arturn/32/2096_2.png) [@arturn](https://discuss.ray.io/u/arturn)
#### Post date: [December 20, 2022, 2:35pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/3 "2022-12-20T14:35:08Z")

</div>

Thanks for this cool code @mannyv !

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 20, 2022, 4:08pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/4 "2022-12-20T16:08:21Z")

</div>

I SHOULD ALSO NOTE THAT I AM USING python3.7 and ray[rllib]==2.0.1.

I ran a quick test with the PythonObjectTrackingCallbacks callback and here are the objects that seem to tick up with the memory usage.

![ram_util_percent_0](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/8/85f1d4d0c1974555f00f8a23ef83d5f6088a0391.png)  
 ![gcobjects_<class 'cell'>_count_mean](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/a/af6347a75402e5e9fb76b0ca53dd869ce6139681.png)  
 ![gcobjects_<class 'function'>_count_mean](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/7/72dadaa083c69fd3b9c688e2bbbcdb53533b6ead.png)  
 ![gcobjects_<class 'tuple'>_count_mean](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/f/f76244c7cba2239740344b4b4ed86563a416b9b6.png)

---

<div class="post-metadata">

### Author: ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)
#### Post date: [December 20, 2022, 4:32pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/5 "2022-12-20T16:32:29Z")

</div>

@MrDracoG,

Well that really doesn’t clear much up does it? Can you share anything about your configuration or setup? A reproduction script?

One thing I would try next is to swap out the real emvironment for a Random Env to try and disentangle if the leak is in the environment or the rl algorithm.

If you have a custom model I would also try switching to a built in model.

> <https://github.com/ray-project/ray/blob/master/rllib/examples/env/random_env.py>

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 20, 2022, 4:59pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/6 "2022-12-20T16:59:18Z")

</div>

I agree that it doesn’t really clear much up. I have a custom model and environment that I am working with. I think it would be hard to get you a reproduction script without sending in the whole repository. Like I said before, I am working inside of a docker container. However, I am currently running a test outside of a container. If that doesn’t clear things up, I will be sure to test a random environment and get you a reproduction script if the memory leak still persists. I’ll post back shortly with info about training outside of docker.

Btw, thank you for taking the time to respond.

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 20, 2022, 5:03pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/7 "2022-12-20T17:03:31Z")

</div>

Here is the ram usage outside of docker ( 2 workers )…

![ram_util_percent_1](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/d/d2d0edd8c115421ae3d35d5632078aaa2a53b6ce.png)

The line seems to be fairly flat and, to me, this seems to signal that training inside of a docker container may be causing the memory leak .

Here is the other thread that referenced docker containers and linux cgroups related to a memory leak: [Help debugging a memory leak in rllib](https://discuss.ray.io/t/help-debugging-a-memory-leak-in-rllib/2100)

I would also like to note that I don’t think there was a memory leak when using a single worker ( and no gpu ) inside of a docker container. I will go back and check that out.

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 20, 2022, 5:45pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/8 "2022-12-20T17:45:27Z")

</div>

Nevermind, even with a single worker there is a memory leak inside of a docker container.

![ram_util_percent_3](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/e/e67abd910a496d122347ddd34b515cb4a337c4d3.png)

---

<div class="post-metadata">

### Author: ![arturn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/arturn/32/2096_2.png) [@arturn](https://discuss.ray.io/u/arturn)
#### Post date: [December 20, 2022, 7:11pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/9 "2022-12-20T19:11:55Z")

</div>

We also run some memory leak tests. Specifically for PPO. So I’d say it’s not super likely that this stems from inside RLlib itself. This can also happen if your rollouts are crazy long I guess.

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 20, 2022, 11:57pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/11 "2022-12-20T23:57:44Z")

</div>

I went back and did a longer (relative to earlier) run with a single worker both inside and outside of docker today and it doesn’t seem like there is a difference for the memory leak, so please ignore what I said earlier about it being in docker.

Inside docker:  
 ![ram_util_percent_4](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/f/fef306756b22f463ad9fa1e54599f29e6d7b1890.png)

Outside docker:  
 ![ram_util_percent_5](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/1/11531ee9267f5600beb84e27a7f444d9c70a7654.png)

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 21, 2022, 12:03am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/12 "2022-12-21T00:03:34Z")

</div>

Yeah, I think that it is possible to be external to RLlib itself.

What would be an example of a crazy long rollout?

I appreciate your response.

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 21, 2022, 12:09am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/13 "2022-12-21T00:09:08Z")

</div>

What are your thoughts on the MemoryTrackingCallbacks callback [[How To Contribute to RLlib — Ray 3.0.0.dev0](https://docs.ray.io/en/master/rllib/rllib-dev.html?highlight=memory%20leak#finding-memory-leaks-in-workers)] returning worker/data\_mean, worker/rss\_mean and vms\_mean as some of the top 20 memory users that also tick up with the ram usage.

I am gonna try to run the training with the MemoryTrackingCallbacks callback longer… just gonna wait until overnight to do it because the callback seems to slow down the training quite a bit.

---

<div class="post-metadata">

### Author: ![arturn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/arturn/32/2096_2.png) [@arturn](https://discuss.ray.io/u/arturn)
#### Post date: [December 21, 2022, 9:06am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/14 "2022-12-21T09:06:50Z")

</div>

I think the first thing you should do would be what @mannyv suggested - replace your env with a random env. It’s very little work.

A crazy long rollout depends on your setting. If your episodes are 10k steps long and you set `rollout_fragment_length` to 10k, RLlib’s sample collection code will buffer these 10k samples for each env you are evaluating on. So for 100 workers, that would be 1M samples. That’d be a lot of memory even for simple atari envs.

---

<div class="post-metadata">

### Author: ![arturn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/arturn/32/2096_2.png) [@arturn](https://discuss.ray.io/u/arturn)
#### Post date: [December 21, 2022, 9:07am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/15 "2022-12-21T09:07:08Z")

</div>

Please use @mannyv’s advice or post a reproduction script.

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 21, 2022, 4:52pm UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/16 "2022-12-21T16:52:46Z")

</div>

I used the RandomEnv class for the environment. It doesn’t seem like the memory leak persists. I guess I’ll have to check my environment again… I don’t believe my episodes are anywhere close to 10k steps long.

![ram_util_percent_6_random_env](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/b/b760423da67249f290d5e4603a4a5e77ad1c869f.png)

Thank you for your help.

---

<div class="post-metadata">

### Author: ![MrDracoG](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mrdracog/32/3378_2.png) [@MrDracoG](https://discuss.ray.io/u/MrDracoG)
#### Post date: [December 24, 2022, 3:41am UTC](https://discuss.ray.io/t/memory-leak-when-training-ppo-on-a-single-agent-environment/8712/17 "2022-12-24T03:41:46Z")

</div>

Can confirm that I found a leak in my environment. Sorry for wasting the time. Thanks for the help!

![ram_util_percent_7](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/2/23515d128d690f22ce3a54ff66a7f7bed7e09726.png)
