Different rss used when running python code in ipython vs script

Viewed 35

I'm trying to get the memory occupied by a snippet. While running it via script gives same output all the time. But running from ipython terminal gives different results each time I try. I tried with a separate process as well and still the same.

Script:

> cat /var/tmp/mem1.py
def a(): 
    import os, psutil  
    process = psutil.Process(os.getpid())  
    before = process.memory_info().rss 
    a = [10]*100000 
    after = process.memory_info().rss 
    print(before, after, after-before) 
                                                       
a()
> python /var/tmp/mem1.py
10035200 10838016 802816
> python /var/tmp/mem1.py
10039296 10842112 802816
> python /var/tmp/mem1.py
10039296 10842112 802816

From ipython terminal

In [1]: def a(): 
   ...:     import os, psutil  
   ...:     process = psutil.Process(os.getpid())  
   ...:     before = process.memory_info().rss 
   ...:     a = [10]*100000 
   ...:     after = process.memory_info().rss 
   ...:     print(before, after, after-before) 
   ...:                                                                                                                                                                                                 

In [2]: from multiprocessing import Process                                                                                                                                                             

In [3]: p = Process(target=a); p.start(); p.join();                                                                                                                                                     
48271360 49057792 786432

In [4]: p = Process(target=a); p.start(); p.join();                                                                                                                                                     
48267264 49053696 786432

In [5]: p = Process(target=a); p.start(); p.join();                                                                                                                                                     
48279552 49033216 753664

In [6]: p = Process(target=a); p.start(); p.join();                                                                                                                                                     
48275456 49041408 765952

Can someone help me understand why I'm seeing this behavior in ipython ?

0 Answers
Related