# Garbage collection through cloudpicikle

**URL:** <https://discuss.ray.io/t/garbage-collection-through-cloudpicikle/7032>\
**Category:** Ray Core\
**Created:** [August 1, 2022, 2:13am UTC](https://discuss.ray.io/t/garbage-collection-through-cloudpicikle/7032 "2022-08-01T02:13:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marsupialtail](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/marsupialtail/32/1950_2.png) [@marsupialtail](https://discuss.ray.io/u/marsupialtail)\
**Post date:** [August 1, 2022, 2:13am UTC](https://discuss.ray.io/t/garbage-collection-through-cloudpicikle/7032/1 "2022-08-01T02:13:16Z")

</div>

I did an interesting experiment. Start two servers with GPUs attached. Set one of them to be head node one of them to worker node, etc.

Now on the worker node run the following code:

```auto
ray.init("auto").
a = torch.randn(10,1024,1024).cuda()
z = ray.put(a)
ray.cloudpickle.dumps(z)

```

Now paste this output to the head node:  
a = ray.get(ray.cloudpickle.loads(OUTPUT\_FROM\_ABOVE))

a is now auto-magically a Torch cuda Tensor. Bravo ray.get!

But now the memory footprint, ~40MB, is on both machines! I have found no way to free this 40MB from either machine. Python del doesn’t work. ray.internal.internal\_api.free doesn’t work.

Is it because I am doing something bad with cloudpickle and it breaks Ray’s reference counting?

---

<div class="post-metadata">

**Author:** ![rickyyx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rickyyx/32/2671_2.png) [@rickyyx](https://discuss.ray.io/u/rickyyx)\
**Post date:** [August 1, 2022, 5:00pm UTC](https://discuss.ray.io/t/garbage-collection-through-cloudpicikle/7032/2 "2022-08-01T17:00:10Z")

</div>

cc @suquark on this - are you aware of such behaviour from ray cloudpickle?

---

<div class="post-metadata">

**Author:** ![suquark](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/suquark/32/227_2.png) [@suquark](https://discuss.ray.io/u/suquark)\
**Post date:** [August 1, 2022, 6:16pm UTC](https://discuss.ray.io/t/garbage-collection-through-cloudpicikle/7032/3 "2022-08-01T18:16:52Z")

</div>

`ray.cloudpickle` is a private API in Ray and should only be used for debugging purpose. Some functionalities of Ray would not work properly when using `ray.cloudpickle` alone.

I think what happened here was `ray.cloudpickle.dumps` pinned the Ray ObjectRef (via `ray._private.serialization.SerializationContext.add_contained_object_ref`). When used inside Ray, it will be unpinned later; but here it will be pinned forever because `ray.cloudpickle.dumps` is used alone.
