Python Garbage Collection sometimes not working in Jupyter Notebook

后端 未结 2 734
深忆病人
深忆病人 2021-02-05 11:26

I\'m constantly running out of RAM with some Jupyter Notebooks and I seem to be unable to release memory that is no longer needed. Here is an example:

import gc
         


        
2条回答
  •  庸人自扰
    2021-02-05 12:06

    I had the same issue, and after many hours of struggle, the solution that worked for me was very lean. You just need to include all your code into a single cell. In the same cell, garbage collection is performed normally, and only after you leave the cell is when the variables have all the extra references and are not collectible.

    For long notebooks, this might be a highly inconvenient and non-readable way, however, the idea is that you can perform garbage collection in a cell for the variables in that cell. So maybe you could organize your code in a way that you can call gc.collect() at the end of the cell before leaving it.

    Hope this helps :)

提交回复
热议问题