tags:

views:

215

answers:

1

In python I have noticed that if you do

mylist = []

for i in range(0,100000000):
    mylist.append('something here to take memory')

mylist = []

it would seem the second call

mylist = []

would remove the reference and it would get collected but, as I watch them memory it does not.

when I use

del mylist[:]

it almost deletes everything but a few megs (just looking at the process)

when I use

del mylist[:]
gc.collect()

I would seem to return to the same amount of memory from before the list was created

so.... why does

mylist = []

not work??? Nothing else is referencing it as far as I can tell

+4  A: 

How do you measure that?

I've made a small test that does not confirm your results. Here is the source:

import meminfo, gc, commands

page_size = int(commands.getoutput("getconf PAGESIZE"))

def stat(message):
    s = meminfo.proc_stat()
    print "My memory usage %s: RSS: %dkb, VSIZE: %dkb" % (
        message, s['rss']*page_size/1024, s['vsize']/1024)
mylist = []

stat("before allocating a big list")
for i in range(0,3000000):
    mylist.append('something here to take memory')

stat("after allocating big list")
### uncomment one of these:
mylist = []
# del mylist[:]
stat("after dropping a big list")
gc.collect()
stat("after gc.collect()")
gc.collect()
stat("after second gc.collect()")
gc.collect()
stat("after third gc.collect()")

The meminfo module used is here: http://gist.github.com/276090

These are the results with mylist=[]:

My memory usage before allocating a big list: RSS: 3396kb, VSIZE: 7324kb
My memory usage after allocating big list: RSS: 50700kb, VSIZE: 55084kb
My memory usage after dropping a big list: RSS: 38980kb, VSIZE: 42824kb
My memory usage after gc.collect(): RSS: 38980kb, VSIZE: 42824kb
My memory usage after second gc.collect(): RSS: 38980kb, VSIZE: 42824kb
My memory usage after third gc.collect(): RSS: 38980kb, VSIZE: 42824kb

This are the results with del mylist[:]:

My memory usage before allocating a big list: RSS: 3392kb, VSIZE: 7324kb
My memory usage after allocating big list: RSS: 50696kb, VSIZE: 55084kb
My memory usage after dropping a big list: RSS: 38976kb, VSIZE: 42824kb
My memory usage after gc.collect(): RSS: 38976kb, VSIZE: 42824kb
My memory usage after second gc.collect(): RSS: 38976kb, VSIZE: 42824kb
My memory usage after third gc.collect(): RSS: 38976kb, VSIZE: 42824kb

Python may allocate memory for its own heap, but it does not necessarily free it immediately after garbage collection.

abbot
ah I need to use that meminfo function. I was just looking at my task manager. Ill post the code
Tim