How to release memory during executing the code?

Tommy_yu: <p>RuntimeError: [enforce fail at …\c10\core\CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 48251840 bytes. Buy new RAM!</p>
<p>There is some data that I don’t need. So how to release them?</p>

albanD: <p>Hi,</p>
<p>For CPU data, just make sure that you cannot access them anymore.<br>
As soon as a Tensor cannot be accessed, it is released.</p>

Tommy_yu: <p>Hi，albanD.<br>
I understand what you said, but I’m not sure if I really don’t access them.<br>
Take the following code as an example.<br>
Could you give me some advice?<br>
I omitted some parameters.</p>
<p>tensor = load_data()<br>
for i in range(50):<br>
{<br>
tensor = gcn_model(tensor)<br>
}</p>
<p>train(tensor)</p>

albanD: <p>In this code for example,<br>
the Tensor0 returned by <code>load_data()</code> is associated with the name “tensor” on the first line.<br>
On the first iteration of the loop, that Tensor0 is given to the gcn_model and a new Tensor1 is returned. It is associated with the name “tensor”.<br>
At this point, no name refers to Tensor0, so it is deleted.<br>
On the second iteration, Tensor1 is given to the model to create Tensor2. This new Tenso2 is associated with the name “tensor” and since Tensor1 cannot be accessed anymore, it is deleted.<br>
Etc</p>

Tommy_yu: <p>Thank you, albanD~ By the way, I use psutil to find out how much memory I use. It seems that I still have a lot of free space to use. Why it prompted such error?</p>

albanD: <p>Which OS are you using? You may be limiting the amount of RAM the process is allowed to allocate?</p>

Tommy_yu: <p>I am using windows 10.  Is there any solution to release the limitation? <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>

albanD: <p>I have to admit I don’t know as I don’t use windows to run pytorch in general.<br>
Maybe <a class="mention" href="/u/peterjc123">@peterjc123</a> would have a better idea here?</p>

peterjc123: <aside class="quote no-group" data-username="Tommy_yu" data-post="4" data-topic="76568">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/3be4f8/48.png" class="avatar"> Tommy_yu:</div>
<blockquote>
<p>load_data()</p>
</blockquote>
</aside>
<p>Would you please post the code?</p>

Tommy_yu: <p>I upload the code to github. Thank you~</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/yutaoming/Code-with-memory-problems" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/31263181?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/yutaoming/Code-with-memory-problems" target="_blank" rel="nofollow noopener">yutaoming/Code-with-memory-problems</a></h3>

<p>Contribute to yutaoming/Code-with-memory-problems development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


peterjc123: <p>Would you please refactor your code into the following style?</p>
<pre><code class="lang-auto">def xxx():
    xxx

def yyy():
    yyy

def main():
    zzz

if __name__ == '__main__':
    main()
</code></pre>

Tommy_yu: <p>I have refactored my code. It didn’t work <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=9" title=":joy:" class="emoji" alt=":joy:"></p>

ecdrid: <p>Re-factoring for easy reading;</p>

peterjc123: <p>Would you please use <code>objgraph</code> for capturing more details? You can refer to this post: <a href="https://benbernardblog.com/tracking-down-a-freaky-python-memory-leak/" rel="nofollow noopener">https://benbernardblog.com/tracking-down-a-freaky-python-memory-leak/</a>.</p>

Tommy_yu: <p>I have added some comment on my code. Thank you for replying. Hope you can give me some advice.</p>

Tommy_yu: <p>OK, I also add some comment on my code for easy understanding.</p>

Tommy_yu: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/8/78a23d7d0b16aac47b72206f90ada509641a5d7d.png" alt="most common types" data-base62-sha1="hdaTshYnTi3NK77pXYI1z72gEEJ" width="488" height="417"><br>
Some details by using objgraph. It seems that only dict increases a little.<br>
In my computer, the code can execute active_learning() function 30 times around.</p>

peterjc123: <p>What about the count of the type <code>Tensor</code>?</p>

Tommy_yu: <p>import objgraph<br>
objgraph.show_most_common_types(limit = 50)<br>
function                   30757<br>
dict                       22992<br>
tuple                      13382<br>
list                       7525<br>
cell                       5955<br>
weakref                    5793<br>
getset_descriptor          4999<br>
method_descriptor          4306<br>
builtin_function_or_method 3694<br>
wrapper_descriptor         3428<br>
type                       3244<br>
property                   2015<br>
set                        1808<br>
module                     1608<br>
ModuleSpec                 1602<br>
SourceFileLoader           1440<br>
LP_DGLArray                1121<br>
staticmethod               1112<br>
member_descriptor          735<br>
WeakSet                    555<br>
classmethod                529<br>
fused_cython_function      495<br>
FontEntry                  471<br>
NDArray                    435<br>
itemgetter                 405<br>
instancemethod             350<br>
MovedAttribute             342<br>
cython_function_or_method  250<br>
frozenset                  224<br>
CopyReduceBackward         216<br>
Enum                       205</p>
<p>It didn’t return the count of the type Tensor</p>