I have a very simple script that allocates memory, dels
the only reference to a sizable object, all the while printing heapy
and pidstat
reports. After running the script, heapy tells me that there should not be much memory being used while pidstat tells me the opposite:
from guppy import hpy
import time
import sys
import os
'''
1) print heapy and pidstat report after starting and before actually doing any work
2) allocate some memory in a simple 2d array
3) print heapy and pidstat report
4) del the d2 array (attempt at garbage collection)
5) print heapy and pidstat report
6) sleep so pidstat can continue to be run to check on memory
'''
def pidstat(msg):
print '==============================='
print msg
os.system('pidstat -r -p %s' % os.getpid())
print '+++++++++++++++++++++++++++++++'
print hpy().heap()[0]
print '==============================='
pidstat('before doing anything')
docs = []
for doc in range(0, 10000):
docs.append([j for j in range(0, 1000)])
pidstat('after fetching all the docs into memory')
del docs
pidstat('after freeing the docs')
time.sleep(60)
The output looks as follows:
=============================== before doing anything Linux 2.6.38-15-generic (hersheezy) 08/14/2012 _x86_64_ (4 CPU) 01:05:20 PM PID minflt/s majflt/s VSZ RSS %MEM Command 01:05:20 PM 5360 0.44 0.00 44768 9180 0.11 python +++++++++++++++++++++++++++++++ Partition of a set of 19760 objects. Total size = 1591024 bytes. Index Count % Size % Cumulative % Kind (class / dict of class) 0 19760 100 1591024 100 1591024 100 str =============================== =============================== after fetching all the docs into memory Linux 2.6.38-15-generic (hersheezy) 08/14/2012 _x86_64_ (4 CPU) 01:05:21 PM PID minflt/s majflt/s VSZ RSS %MEM Command 01:05:21 PM 5360 8.95 0.00 318656 279120 3.49 python +++++++++++++++++++++++++++++++ Partition of a set of 7431665 objects. Total size = 178359960 bytes. Index Count % Size % Cumulative % Kind (class / dict of class) 0 7431665 100 178359960 100 178359960 100 int =============================== =============================== after freeing the docs Linux 2.6.38-15-generic (hersheezy) 08/14/2012 _x86_64_ (4 CPU) 01:05:29 PM PID minflt/s majflt/s VSZ RSS %MEM Command 01:05:29 PM 5360 40.23 0.00 499984 460480 5.77 python +++++++++++++++++++++++++++++++ Partition of a set of 19599 objects. Total size = 1582016 bytes. Index Count % Size % Cumulative % Kind (class / dict of class) 0 19599 100 1582016 100 1582016 100 str ===============================
How can I make sure this memory is returned to the operating system?
Unlike many other languages, Python does not necessarily release the memory back to the Operating System. Instead, it has a dedicated object allocator for objects smaller than 512 bytes, which keeps some chunks of already allocated memory for further use in the future.
If you don't manually free() the memory allocated by malloc() , it will never get freed. Python, in contrast, tracks objects and frees their memory automatically when they're no longer used. But sometimes that fails, and to understand why you need to understand how it tracks them.
Clear Memory in Python Using the del Statement Along with the gc. collect() method, the del statement can be quite useful to clear memory during Python's program execution. The del statement is used to delete the variable in Python.
to clear Memory in Python just use del. By using del you can clear the memory which is you are not wanting. By using del you can clear variables, arrays, lists etc.
There can be a difference between when memory is made available for reuse inside the python
process and when it is released to the OS. In particular, the standard Python interpreter (CPython) maintains its own pools and free lists for particular kinds of objects. It will reuse memory in these pools itself, but never releases it to the OS once it's been used.
See this for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With