# Collecting garbage questions

**URL:** <https://discuss.pytorch.org/t/collecting-garbage-questions/56821>\
**Category:** Uncategorized\
**Created:** [September 25, 2019, 6:30pm UTC](https://discuss.pytorch.org/t/collecting-garbage-questions/56821 "2019-09-25T18:30:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackbirdbarber](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@blackbirdbarber](https://discuss.pytorch.org/u/blackbirdbarber)\
**Post date:** [September 25, 2019, 6:30pm UTC](https://discuss.pytorch.org/t/collecting-garbage-questions/56821/1 "2019-09-25T18:30:53Z")

</div>

I found:

```auto
import torch
import gc
for obj in gc.get_objects(): 
    try:
        if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
            print(type(obj), obj.size())
    except: pass

```

Are these objects `gc.get_objects()` ready to be collected or they will not be collected unless we set them to None inside of the program?

Then I found:

```auto
# prints currently alive Tensors and Variables
import torch
import gc
for obj in gc.get_objects():
    if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
        del obj
torch.cuda.empty_cache()

```

What will this code do?  
Is `del obj` same as `obj=None`.  
What is the difference between `torch.cuda.empty_cache()` and `gc.collect()`.
