Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could also use the <code>getrusage()</code> function from the standard library module <code>resource</code>. The resulting object has the attribute <code>ru_maxrss</code>, which gives total memory usage for the calling process:</p> <pre><code>&gt;&gt;&gt; import resource &gt;&gt;&gt; resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 2656 </code></pre> <p>The <a href="http://docs.python.org/library/resource.html#resource-usage" rel="noreferrer">Python docs</a> aren't clear on what the units are exactly, but the <a href="http://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/man2/getrusage.2.html" rel="noreferrer">Mac OS X man page</a> for <code>getrusage(2)</code> describes the units as kilobytes.</p> <p>The Linux man page isn't clear, but it seems to be equivalent to the <code>/proc/self/status</code> information (i.e. kilobytes) described in the accepted answer. For the same process as above, running on Linux, the function listed in the accepted answer gives:</p> <pre><code>&gt;&gt;&gt; memory_usage() {'peak': 6392, 'rss': 2656} </code></pre> <p>This may not be quite as easy to use as the <code>/proc/self/status</code> solution, but it is standard library, so (provided the units are standard) it should be cross-platform, and usable on systems which lack <code>/proc/</code> (eg Mac OS X and other Unixes, maybe Windows).</p> <p>Also, <code>getrusage()</code> function can also be given <code>resource.RUSAGE_CHILDREN</code> to get the usage for child processes, and (on some systems) <code>resource.RUSAGE_BOTH</code> for total (self and child) process usage.</p> <p>This will cover the <code>memory_get_usage()</code> case, but doesn't include peak usage. I'm unsure if any other functions from the <code>resource</code> module can give peak usage. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload