Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

release memory after del in jupiter notebook

I have deleted some variables in jupiter notebook using del list_of_df. But we realize the contents still occupies memory. so we tried %reset list_of_df , but the previous variable names are already not there... Is there nothing we could do but to restart the kernel to recollect the memory? Thanks

Further: In a more general, I might have lost track of what I have deleted from a huge jupiter notebook codes. Is it possible to check what have been deleted but still occupying the memory and delete it?

like image 398
user40780 Avatar asked Oct 26 '25 03:10

user40780


1 Answers

Python isn't like C (for example) in which you have to manually free memory. Instead, all memory allocation and deallocation tasks are handled automatically in the background by a garbage collection (GC) routine. GC uses lazy evaluation, which means that the memory probably won't be freed right away, but will instead be freed only when it "needs" to be (in the ideal case, anyway).

You shouldn't use this in production code, but if you really want to, after you del your list you can force GC to run using the gc module:

import gc
gc.collect()

It might not actually work/deallocate the memory, though, for many different reasons. In general, it's better to just let Python manage memory automatically and not interfere.

like image 182
tel Avatar answered Oct 29 '25 09:10

tel