How to free memory of python deleted object?

后端 未结 1 534
旧时难觅i
旧时难觅i 2021-02-19 21:03

It seems python3.5 does not completely free memory of any deleted object, this may because of python internally maintaining some kind of memroy pool for reusing purpose, however

1条回答
  •  一向
    一向 (楼主)
    2021-02-19 21:53

    Once you delete an object it is available to garbage collected rather than deleted immediately - so just give it some time and it will free up or trigger a gc.collect() to speed things up.

    python.exe
    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import psutil
    >>> import gc
    >>> psutil.Process().memory_info().rss / 2**20
    13.2890625
    >>> d = {x:x for x in range(10**7)}
    >>> psutil.Process().memory_info().rss / 2**20
    359.13671875
    >>> del d
    >>> psutil.Process().memory_info().rss / 2**20
    13.5234375
    >>> gc.collect()
    0
    >>> psutil.Process().memory_info().rss / 2**20
    13.4375
    >>>
    

    Just for reference the Python 3 shell is actually more like ipython 2 in that there is a certain amount of storage taken up with history, etc., just for reference:

    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import psutil
    >>> psutil.Process().memory_info().rss / 2**20
    13.1875
    >>> psutil.Process().memory_info().rss / 2**20
    13.20703125
    >>> psutil.Process().memory_info().rss / 2**20
    13.20703125
    >>> psutil.Process().memory_info().rss / 2**20
    13.20703125
    >>> psutil.Process().memory_info().rss / 2**20
    13.20703125
    >>> 22*3
    66
    >>> psutil.Process().memory_info().rss / 2**20
    13.25390625
    >>> import gc
    >>> psutil.Process().memory_info().rss / 2**20
    13.25390625
    >>> gc.collect()
    0
    >>> psutil.Process().memory_info().rss / 2**20
    13.171875
    >>>
    

    Next Morning to check if doing dict update in a function is different:

    >>> psutil.Process().memory_info().rss / 2**20
    13.1484375
    >>> D = {}
    >>> psutil.Process().memory_info().rss / 2**20
    13.1484375
    >>> def UpdateD(d, v):
    ...     """ Add the text and value for v to dict d """
    ...     d[v] = str(v)
    ...
    >>> psutil.Process().memory_info().rss / 2**20
    13.16015625
    >>> for x in range(10**7):
    ...     UpdateD(D, x)
    ...
    >>> psutil.Process().memory_info().rss / 2**20
    666.6328125
    >>> del D
    >>> psutil.Process().memory_info().rss / 2**20
    10.765625
    >>> gc.collect()
    0
    >>> psutil.Process().memory_info().rss / 2**20
    12.8984375
    >>>
    

    So it looks like your production code might be hanging onto a reference that you still have to track down.

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