# Tensor to Variable and memory freeing best practices

**URL:** <https://discuss.pytorch.org/t/tensor-to-variable-and-memory-freeing-best-practices/6000>\
**Category:** Uncategorized\
**Created:** [August 9, 2017, 7:50pm UTC](https://discuss.pytorch.org/t/tensor-to-variable-and-memory-freeing-best-practices/6000 "2017-08-09T19:50:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![fmassa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/32/58_2.png) [@fmassa](https://discuss.pytorch.org/u/fmassa)\
**Post date:** [August 9, 2017, 9:20pm UTC](https://discuss.pytorch.org/t/tensor-to-variable-and-memory-freeing-best-practices/6000/2 "2017-08-09T21:20:18Z")

</div>

`Variable` share the same memory as its underlying `Tensor`, so there is no memory savings by deleting it afterwards.  
That being said, you can also replace the `Tensor` variable with a `Variable` containing the `Tensor`. E.g.,

```python
x = torch.rand(5)
x = Variable(x)

```

The only case where you might see some (small) savings is when you reach the end of the training loop, you might want to delete all references to the input tensor.

```python
for i, (x, y) in enumerate(train_loader):
    x = Variable(x)
    y = Variable(y)
    # compute model and update
    del x, y, output 

```

This ensures that you won’t have double the memory necessary for `x` and `y`, because `train_loader` first allocates the memory, and then assign it to `x`.  
But note that if your input tensor is relatively small, this saving by doing that is negligible and not worth it (you also need to make sure that all references to `x` are deleted, that’s why I `del output` as well).

---

_[View the full topic](https://discuss.pytorch.org/t/tensor-to-variable-and-memory-freeing-best-practices/6000)._
