How to prevent leak when using .cuda() on temporary view and replacing original variable?

Hiro_Protagonist: <p>n = 10<br>
x = torch.zeros(n)<br>
x = x[:n//2].cuda()</p>
<h1>
<a name="at-this-point-we-have-leaked-memory-on-the-cpu-1" class="anchor" href="#at-this-point-we-have-leaked-memory-on-the-cpu-1"></a>at this point, we have leaked memory on the cpu</h1>
<p>I am using this gist: <a href="https://gist.github.com/Stonesjtu/368ddf5d9eb56669269ecdf9b0d21cbe" class="inline-onebox" rel="noopener nofollow ugc">A simple Pytorch memory usages profiler · GitHub</a><br>
to profile. It basically traverses objects accessible by gc and checks where they are.</p>
<p>x points to a buffer on gpu</p>
<pre><code class="lang-auto">
In [5]: mem_report()                                               
=================================================================
Element type    Size                    Used MEM(MBytes)
Storage on GPU
-----------------------------------------------------------------
Tensor          (5,)            0.00
-----------------------------------------------------------------
Total Tensors: 5        Used Memory Space: 0.00 MBytes
-----------------------------------------------------------------
Storage on CPU
-----------------------------------------------------------------
Tensor          (10,)           0.00
-----------------------------------------------------------------
Total Tensors: 10       Used Memory Space: 0.00 MBytes
-----------------------------------------------------------------
=================================================================
</code></pre>

ptrblck: <p>I cannot reproduce this behavior and see:<br>
Before running your code:</p>
<pre><code class="lang-python">=================================================================
Element type	Size			Used MEM(MBytes)
Storage on GPU
-----------------------------------------------------------------
-----------------------------------------------------------------
Total Tensors: 0 	Used Memory Space: 0.00 MBytes
-----------------------------------------------------------------
Storage on CPU
-----------------------------------------------------------------
-----------------------------------------------------------------
Total Tensors: 0 	Used Memory Space: 0.00 MBytes
-----------------------------------------------------------------
=================================================================
</code></pre>
<p>After:</p>
<pre><code class="lang-python">=================================================================
Element type	Size			Used MEM(MBytes)
Storage on GPU
-----------------------------------------------------------------
Tensor		(5,)		0.00
-----------------------------------------------------------------
Total Tensors: 5 	Used Memory Space: 0.00 MBytes
-----------------------------------------------------------------
Storage on CPU
-----------------------------------------------------------------
-----------------------------------------------------------------
Total Tensors: 0 	Used Memory Space: 0.00 MBytes
-----------------------------------------------------------------
=================================================================
</code></pre>

Hiro_Protagonist: <p>Sorry I think I was an in an interactive ipython session that somehow had another reference to the old variable.</p>
<p>Thank you for double checking this. Sorry for the false alarm.</p>