ENH added optional tracemalloc backend#117
Conversation
It is now possible to use ``tracemalloc`` to analyze memory usage Python
code on Python 3.4 and above. ``tracemalloc`` allows for more precise
measurements compared to ``psutil``. However, it only works either for
pure Python code or for C extensions allocating memory via ``PyMem_Alloc``.
To use the new backend code run ``memory_profiler`` with ``--backend``
option, e.g.
$ python -m memory_profiler --backend=tracemalloc script.py
Also if you use ``memory_profiler`` with imported decorator you can specify
backend as an argument to the decorator function:
@Profile(backend='tracemalloc')
def f(n):
a = [0] * n
return a
``backend`` parameter to ``@profile`` has priority over ``--backend``.
Note that using ``tracemalloc`` in ``mprof`` and IPython magic is not
supported at the moment.
|
|
||
| _backend_chosen = False | ||
| _backend = 'psutil' | ||
|
|
There was a problem hiding this comment.
Do you need two global variables? I think you can do with only one (possibly initialized to None).
|
Thanks for the contribution!! Looks great. If I understood it correctly, the default backends by precedence are psutil -> posix (if on posix) -> tracemalloc, right? (this is perfect, just want to be sure I understood). Also, what do you mean by all those "# TODO: add filename" ? could you remove them if they are not important? |
| _backend = n_backend | ||
| break | ||
| if _backend == 'no_backend': | ||
| raise NotImplementedError('Tracemalloc or psutil module is required for non-unix ' |
There was a problem hiding this comment.
A RuntimeError would be more appropriate here I think.
@fabianp, yes you understood correctly
That TODO i added in places where necessary add some code to support tracemalloc, but i not invented now how realize that |
|
I don't understand your last statement. Do you mean that the support for tracemalloc is not complete? |
|
Yes, you right. tracemalloc now works only in profiling with
|
|
@demiurg906 let me know when you think this is ready to be merged |
|
Hi @fabianp, I wanted to quickly summarize the status of this PR. The good part: The not-so-good parts:
The last issue can be partially "solved" by restarting tracing after each line of code. This brings Another way to approach the speed issue is to write a stripped down version of I think deallocations are a must-have and therefore the only real option we have is to write a custom allocator. What do you think? |
|
thanks for the update @superbobry . For now I would like to avoid including any compiled code. For me, the benefit in speed would not compensate the deployment and maintenance burden. I think the current code is good enough, and we can always improve upon. Is there something left to do? |
| self.include_children = kw.pop("include_children", False) | ||
|
|
||
| # get baseline memory usage | ||
| # TODO: add filename |
There was a problem hiding this comment.
@demiurg906 is this (and the following) TODO still relevant?
|
@fabianp, I fix all remaining comments, so I think PR is ready to merge |
| return OrderedDict(items) | ||
| if new_backend is not None: | ||
| backends = move_to_start(backends, new_backend) | ||
| backends.insert(0, backends.pop(backends_indices[new_backend])) |
There was a problem hiding this comment.
After this line backends_indices will become invalid, right?
Update: looks like backends_indices isn't used anywhere in the code.
There was a problem hiding this comment.
right, backends_indices is used only in moving chosen backend
|
Great work @demiurg906! . I've added a commit on top of yours that removes the global variable |
It is now possible to use
tracemallocto analyze memory usage Pythoncode on Python 3.4 and above.
tracemallocallows for more precisemeasurements compared to
psutil. However, it only works either forpure Python code or for C extensions allocating memory via
PyMem_Alloc.To use the new backend code run
memory_profilerwith--backendoption, e.g.
Also if you use
memory_profilerwith imported decorator you can specifybackend as an argument to the decorator function:
backendparameter to@profilehas priority over--backend.Note that using
tracemallocinmprofand IPython magic is notsupported at the moment.