Is there a way to delete created variables, functions, etc from the memory of the interpreter?

前端 未结 6 1936
春和景丽
春和景丽 2020-11-27 10:07

I\'ve been searching for the accurate answer to this question for a couple of days now but haven\'t got anything good. I\'m not a complete beginner in programming, but not y

相关标签:
6条回答
  • 2020-11-27 10:39

    If you are in an interactive environment like Jupyter or ipython you might be interested in clearing unwanted var's if they are getting heavy.

    The magic-commands reset and reset_selective is vailable on interactive python sessions like ipython and Jupyter

    1) reset

    reset Resets the namespace by removing all names defined by the user, if called without arguments.

    in and the out parameters specify whether you want to flush the in/out caches. The directory history is flushed with the dhist parameter.

    reset in out
    

    Another interesting one is array that only removes numpy Arrays:

    reset array
    

    2) reset_selective

    Resets the namespace by removing names defined by the user. Input/Output history are left around in case you need them.

    Clean Array Example:

    In [1]: import numpy as np
    In [2]: littleArray = np.array([1,2,3,4,5])
    In [3]: who_ls
    Out[3]: ['littleArray', 'np']
    In [4]: reset_selective -f littleArray
    In [5]: who_ls
    Out[5]: ['np']
    

    Source: http://ipython.readthedocs.io/en/stable/interactive/magics.html

    0 讨论(0)
  • 2020-11-27 10:39

    Actually python will reclaim the memory which is not in use anymore.This is called garbage collection which is automatic process in python. But still if you want to do it then you can delete it by del variable_name. You can also do it by assigning the variable to None

    a = 10
    print a 
    
    del a       
    print a      ## throws an error here because it's been deleted already.
    

    The only way to truly reclaim memory from unreferenced Python objects is via the garbage collector. The del keyword simply unbinds a name from an object, but the object still needs to be garbage collected. You can force garbage collector to run using the gc module, but this is almost certainly a premature optimization but it has its own risks. Using del has no real effect, since those names would have been deleted as they went out of scope anyway.

    0 讨论(0)
  • 2020-11-27 10:41

    This worked for me.

    You need to run it twice once for globals followed by locals

    for name in dir():
        if not name.startswith('_'):
            del globals()[name]
    
    for name in dir():
        if not name.startswith('_'):
            del locals()[name]
    
    0 讨论(0)
  • 2020-11-27 10:43

    You can use python garbage collector:

    import gc
    gc.collect()
    
    0 讨论(0)
  • 2020-11-27 10:51

    Yes. There is a simple way to remove everything in iPython. In iPython console, just type:

    %reset
    

    Then system will ask you to confirm. Press y. If you don't want to see this prompt, simply type:

    %reset -f
    

    This should work..

    0 讨论(0)
  • 2020-11-27 10:52

    You can delete individual names with del:

    del x
    

    or you can remove them from the globals() object:

    for name in dir():
        if not name.startswith('_'):
            del globals()[name]
    

    This is just an example loop; it defensively only deletes names that do not start with an underscore, making a (not unreasoned) assumption that you only used names without an underscore at the start in your interpreter. You could use a hard-coded list of names to keep instead (whitelisting) if you really wanted to be thorough. There is no built-in function to do the clearing for you, other than just exit and restart the interpreter.

    Modules you've imported (import os) are going to remain imported because they are referenced by sys.modules; subsequent imports will reuse the already imported module object. You just won't have a reference to them in your current global namespace.

    0 讨论(0)
提交回复
热议问题