# How to trigger ray garbage collector manually

**URL:** <https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197>\
**Category:** Ray Core\
**Created:** [May 20, 2022, 12:13am UTC](https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197 "2022-05-20T00:13:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tech\_wanderer](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@tech\_wanderer](https://discuss.ray.io/u/tech_wanderer)\
**Post date:** [May 20, 2022, 12:13am UTC](https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197/1 "2022-05-20T00:13:30Z")

</div>

I’m trying to measure the memory usage of some operations in my application, which uses Modin, which is run through a Ray cluster on my MacBook. For measuring the memory usage, I run `ray.available_resources()["object_store_memory"]` before and after executing each operation. I don’t want the garbage collector to be called during one of the executing operations, which would cause wrong memory measurements. Is there any way to trigger the ray garbage collector manually, so I do it before executing each operation?

---

<div class="post-metadata">

**Author:** ![Mingwei](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mingwei/32/997_2.png) [@Mingwei](https://discuss.ray.io/u/Mingwei)\
**Post date:** [May 20, 2022, 4:44pm UTC](https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197/2 "2022-05-20T16:44:54Z")

</div>

You probably know already but Ray’s object store only contains data stored via `ray.put()`, task args and return values. So `object_store_memory` does not include all memory usages of the application, e.g. Python dicts created in the application.

Ray uses distributed reference counting to manage objects in the object store, and the reference counting is tied to Python GC. So to answer your question, to delete objects in the Ray object store, you can just call `del` on the object reference from Python. An example:

```auto
>>> a = ray.put("zzzz")
>>> ray.available_resources()["object_store_memory"]
37699821139.0
>>> b = ray.put("yyy")
>>> ray.available_resources()["object_store_memory"]
37699821121.0
>>> del b
>>> ray.available_resources()["object_store_memory"]
37699821139.0
>>> del a
>>> ray.available_resources()["object_store_memory"]
37699821158.0

```

---

<div class="post-metadata">

**Author:** ![tech\_wanderer](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@tech\_wanderer](https://discuss.ray.io/u/tech_wanderer)\
**Post date:** [May 24, 2022, 5:43pm UTC](https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197/3 "2022-05-24T17:43:16Z")

</div>

Thank you for your help, Mingwei! I have a few more questions, I’d appreciate it if you could help me with them.

1. Does the garbage collection always happen **immediately** after calling del on the object?

2. If an object has references to “many” other objects and we delete that object, all those objects referenced by it, get deleted altogether immediately (assuming no other object references to them)?

3. Do you know any resource to recommend to study more about _distributed_ reference counting?

---

<div class="post-metadata">

**Author:** ![Mingwei](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mingwei/32/997_2.png) [@Mingwei](https://discuss.ray.io/u/Mingwei)\
**Post date:** [May 24, 2022, 8:15pm UTC](https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197/4 "2022-05-24T20:15:19Z")

</div>

Glad to help!

1. After calling `del` on the object, the local reference count of the underlying object is decremented synchronously. If the local reference count of the object becomes zero, and the object is owned by another node, this information is propagated asynchronously to the owner.

2. Yes, reference counting is done for nested ObjectRefs as well. Similar to above, local reference counting is done synchronously with `del`. Remote reference counting information is propagated asynchronously.

3. [Objects — Ray 3.0.0.dev0](https://docs.ray.io/en/master/ray-core/objects.html) and [Memory Management — Ray 1.12.1](https://docs.ray.io/en/latest/ray-core/objects/memory-management.html) have high level information for the distributed reference counting. More details are available at [https://stephanie-wang.github.io/pdfs/nsdi21-ownership.pdf](https://stephanie-wang.github.io/pdfs/nsdi21-ownership.pdf)

---

<div class="post-metadata">

**Author:** ![tech\_wanderer](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@tech\_wanderer](https://discuss.ray.io/u/tech_wanderer)\
**Post date:** [May 26, 2022, 11:54pm UTC](https://discuss.ray.io/t/how-to-trigger-ray-garbage-collector-manually/6197/5 "2022-05-26T23:54:35Z")

</div>

Thank you very much again, Mingwei! Those resources are also very helpful.

Just to confirm, lets’ say, we have a reference to a big dataset. We have one reference to that dataset and we del that. The garbage collection happens synchronously, correct? or it happens sometime after returning from the del command?
