How to explicitly release object after creating it with `ray.put`?

Viewed 886

I'm trying to get rid of object pinned in shared memory using ray.put. Here is code sample:

import ray
<create obj>

for ...:
  obj_id = ray.put(obj)

  <do stuff with obj_id on ray Actors using ray.get(obj_id)>
  del obj_id

After this is finished, I look at ray dashboard and see that all obj_id are still in ray shared memory with reference type LOCAL_REFERENCE.

Official docs do not elaborate on whether there is any way of explicitly controlling object lifetime. As far as I understood, it basically suggests to wait until all memory is used, and then rely on ray to clean things up.

Question: how do I explicitly purge object from ray shared memory?

Note: I'm using Jupyter, can it be the case that this object is still alive due to this fact?

1 Answers

The function ray.internal.internal_api.free() performs this function. I can't find any documentation on the Ray docs for this function but it has a good docstring you can find here which I've copy-pasted below.

Free a list of IDs from the in-process and plasma object stores.

This function is a low-level API which should be used in restricted
scenarios.

If local_only is false, the request will be send to all object stores.

This method will not return any value to indicate whether the deletion is
successful or not. This function is an instruction to the object store. If
some of the objects are in use, the object stores will delete them later
when the ref count is down to 0.

Examples:
    >>> x_id = f.remote()
    >>> ray.get(x_id)  # wait for x to be created first
    >>> free([x_id])  # unpin & delete x globally

Args:
    object_refs (List[ObjectRef]): List of object refs to delete.
    local_only (bool): Whether only deleting the list of objects in local
    object store or all object stores.
Related