credtz
credtz

Reputation: 47

Memory not released, even as object ref count 0

In the following code snippet, x is no longer defined however the memory it took has not been released. Why?

import psutil
import os
def memtest():
    process = psutil.Process(os.getpid())
    x = (process.memory_info().rss)/10**6
    return float(x)

def myfunc():
    x = [2]*10**7
    return 0

print(memtest())
myfunc()
print(memtest())

Output:

9.519104
89.546752

Based on the idea of reference counting, x has its reference count increase by 1 when it is defined in the local scope of the myfunc, as soon as we leave that scope, it has its reference count decrease by 1 and so the memory associated with that object should have been freed. Why is this not the case?

EDIT:

Even after running del x the memory is not freed. Why?

import psutil
import os
def memtest():
    process = psutil.Process(os.getpid())
    x = (process.memory_info().rss)/10**6
    return float(x)

def myfunc():
    x = [2]*10**7
    del x
    return 0

print(memtest())
myfunc()
print(memtest())

Output

9.629696
89.657344

Upvotes: 0

Views: 261

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155363

Allocators don't always release memory back to the OS; they'll hang on to it to satisfy future allocations without needing to rerequest memory from the OS (much more expensive than partitioning existing allocations). Try calling your function a few times in a row; if the memory usage keeps increasing, okay, maybe there is a problem, but in most cases, most of the memory used in the future comes from what you released earlier.

To be clear, del does nothing of use in your code. All it does is release the current reference to the list bound to x. Since x is a purely local variable that you don't return, that reference would be released at the moment you return anyway. del of bare names is rarely useful (outside of rare cases involving temporaries at global scope that you want to get rid of after they've been used in some other global initialization), since the deled name would usually disappear in short order anyway.

Upvotes: 1

Related Questions