Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to tackle the problem if you are calling lots of different functions and you are unsure where the swapping comes from would be to use the new plotting functionality from <a href="https://pypi.python.org/pypi/memory_profiler" rel="noreferrer">memory_profiler</a>. First you must decorate the different functions you are using with @profile. For simplicity I'll use the example <a href="https://github.com/fabianp/memory_profiler/blob/master/examples/numpy_example.py" rel="noreferrer">examples/numpy_example.py</a> shipped with memory_profiler that contains two functions: <code>create_data()</code> and <code>process_data()</code></p> <p>To run your script, instead of running it with the Python interpreter, you use the mprof executable, that is</p> <pre><code>$ mprof run examples/numpy_example.py </code></pre> <p>This will create a file called <code>mprofile_??????????.dat</code>, where the ? will hold numbers representing the current date. To plot the result, simply type <code>mprof plot</code> and it will generate a plot similar to this (if you have several .dat files it will always take the last one):</p> <p><img src="https://i.stack.imgur.com/6c3G9.png" alt="output of memory_profiler&#39;s mprof"></p> <p>Here you see the memory consumption, with brackets indicating when you enter/leave the current function. This way it is easy to see that function <code>process_data()</code> has a peak of memory consumption. To further dig into your function, you could use the line-by-line profiler to see the memory consumption of each line in your function. This is run with</p> <pre><code>python -m memory_profiler examples/nump_example.py </code></pre> <p>This would give you an output similar to this:</p> <pre><code>Line # Mem usage Increment Line Contents ================================================ 13 @profile 14 223.414 MiB 0.000 MiB def process_data(data): 15 414.531 MiB 191.117 MiB data = np.concatenate(data) 16 614.621 MiB 200.090 MiB detrended = scipy.signal.detrend(data, axis=0) 17 614.621 MiB 0.000 MiB return detrended </code></pre> <p>where it is clear that scipy.signal.detrend is allocating a huge amount of memory.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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