3

I am trying to find out how to see within a Python script (without any external lib) the RAM currently used by this script.

Some search here point me to the resource module: http://docs.python.org/2/library/resource.html#resource-usage

And here, I see there is 2 kind of memory, shared and unshared.

I was wondering what they were describing ? Hard drive versus RAM ? or something about multi-thread memory ? Or something else ?

Also, I do not think this is actually helping me to find out the current RAM usage, right ?

Thanks

azerty
  • 698
  • 7
  • 28
  • Can you check this post? http://stackoverflow.com/a/938800/1982962 – Kobi K Oct 09 '13 at 11:33
  • The solution you point out seem quite dirty in my opinion, isn't there a one line way of doing this ? – azerty Oct 09 '13 at 11:43
  • Maybe using getrusage() function from the standard library module resource. resource.getrusage(resource.RUSAGE_SELF).ru_maxrss I have to say I don't know if there is a perfect or a clean solution to this question if someone have a better idea it could help me too. – Kobi K Oct 09 '13 at 11:48
  • The ru_maxrss will only return the MAXimum which was reach and not the current memory usage. Correct me if I am wrong – azerty Oct 09 '13 at 12:18
  • I think it's total memory usage for the calling process. at least this what i understand from the doc at http://docs.python.org/2/library/resource.html#resource-usage – Kobi K Oct 09 '13 at 12:22

1 Answers1

3

RAM is allocated in chunks called pages. Some of these pages can be marked read-only, such as those in the text segment that contain the program's instructions. If a page is read-only, it is available to be shared between more than one process. This is the shared memory you see. Unshared memory is everything else that is specific to the currently running process, such as allocations from the heap.

Ben
  • 683
  • 3
  • 5
  • So both (shared and unshared) are stored in the RAM ? – azerty Oct 09 '13 at 12:20
  • Shared and unshared are both descriptions of how RAM is being used. So yes. It is actually more complicated than that, since the operating system can swap pages of ram to disk, but yes. – Ben Oct 09 '13 at 12:30