Where jupyterlab store variable if it does not assign ? And how it remove?

Hello everyone !

When I do multiplication matrices and does not assign result any variable it increase memory usage

I can find this tensors in gc objects


but can’t remove its

Same question was here but without answer python - GPU memory usage accumulating when calling function even though tensor isn’t stored - Stack Overflow

Where jupyterlab store variable if it does not assign ? And how it remove ?

I removed it from _oh, Out, locals. I did %clear but it did not removed from memory. I am not sure but I think that it is impossible. That better always assign result some variable and then remove this reference

This is not a feature of JupyterLab itself, but of the kernel that you are using (and different Python kernels may or may not behave the same). You are most likely using the ipykernel kernel which is a kernel based on top of IPython.

In IPython the result of last execution is stored in a special _ variable, which is useful if you run an expensive computation and forget to assign (and want to retrieve it afterwards); results of penultimate execution are stored in __ and of one before in ___. In addition results of all previous computations are stored in _oh == Out dictionary (output history, bound to two names) and on _o<n> variables. Please see IPython reference — IPython 8.0.1 documentation for more information.

Now, you can either remove all references to the output you want to purge, or use a magic that does it for you. Assuming it’s the last result which is the 3rd execution result, it would be something like:

del _
del _3
del _oh[3]

But this is a bit crude so instead there are helper magics for you like:

# remove all references to the most recent output
%xdel _

or if you want to remove everything:

# remove entire output history (will prompt for confirmation)
%reset out

Finally you can just disable the caching feature by setting InteractiveShell.cache_size to 0.

You may need to run garbage collection with gc.collect() if you want the removed objects to be cleared immediately (which is usually not needed). This will not reduce the memory usage of your kernel for your OS though - this is very rare to see de-allocation of already allocated memory (but it depends on the operating system). Instead it will allow you to load as large an object to your environment as you just removed.


Thanks a lot!

%xdel _3 helped me

1 Like