You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is currently very hard to implement custom leak checking because it seems impossible to walk the jemalloc heap.
The leak checking of jemalloc itself is very slow and does not print debugging information.
Therefore there should be a public API to walk live objects per arena.
I've implemented precise leak checking in multiple systems, and after long experience with them I no longer consider precise leak enumeration to be an important feature in malloc implementations. These mechanisms have a perfection-based false allure, but in practice they are extremely difficult to utilize.
How do you tell the difference between useful allocations and leaks? Unless you programmatically walk/track the reference graph to distinguish live/dead objects, it's infeasible to recognize leaks for non-toy applications. Okay, so what does work well? I know of three systems that are powerful and practical:
Valgrind. It's slow, but it sure does a good job of detecting leaks (by tracking the reference graph), and it provides great diagnostics on where leaks originated.
AddressSanitizer. Although similarly powerful to Valgrind with regard to detecting memory errors, it has the advantage of much lower CPU overhead, and the disadvantage of being more invasive.
tcmalloc's and jemalloc's heap profiling. Both allocators implement the most critical aspects of this, though jemalloc has expanded on the original feature set of tcmalloc.
Regarding your criticisms of jemalloc's leak checking, i.e. heap profiling:
Slow: It's only slow if you "sample" all allocations, but for non-toy applications it works very well to use a sample rate that has only a small performance impact.
Does not print debugging information: You need to use the jeprof script to post-process heap profiles, and the result is a detailed representation of call graphs associated with extant allocations.
All that said, I'm not totally against enumeration mechanisms, and in fact I have other long term plans that might require this functionality (see GC-related discussion in #203). But, it's not a priority right now, and it's really tricky to provide an API that doesn't suffer from extreme fragility and bad interactions with allocator internals.
Re: the Android patch, it's a bit odd that the application has to prime iteration with addresses. But, jemalloc doesn't currently track non-huge chunks, because it would only need such tracking for live object iteration.
Well, for us heap walk has been vital. We work with >20GB of tiny objects and when certain arenas do not go back to 0, we know that a leak has occurred, but we don't know where.
Of course global heap walk is imperfect as it gives many false positives, but it's quite easy to visually distinguish those from actual leaks, if one know from where in the code it was allocated (and especially if arena IDs are also reported).
Jemalloc's profiling with sampling set to collect every allocation slows down our process (which is already slow) by a factor of 8, so that's impractical to use.
We had a compile-time setting to add the location in the code where the allocation occurred to each block, and this performed relatively well. But without heap walk functionality we can't use this mechanism anymore.
So to be clear, jemalloc's profiling almost solves our problem, but is too slow. My request is therefore to either extend the profiling API with a heap walk API or to provide hooks so I can supply custom call stack info (which is where I think the biggest performance impact is). Note - we are on Windows, if that matters. And if it doesn't report the arena, then to add that as well (haven't seen arenas being reported, but might have missed it).
@rustyx, I still don't understand why it's important in your use case to "sample" every allocation. In my experience, much lower sample rates suffice for detecting all nontrivial leaks, so I'm still wondering if you're making leak detection harder than it needs to be, or if there's something unusual about your operating environment.
I don't know if that's unusual but we have a combination of long-lived data cubes and short-lived requests in the same process. Data cubes are large and are updated just a few times a day. If there is a leak in there of just a single allocation (which we did have in the past), sampling would be highly inefficient in finding it. With a "sample" rate of 8, I would have to run my process 8 times to catch it, and even then there is no guarantee that I will catch it.
Sampling just does not work for us. Live object enumeration on the other hand works very well; an automated process builds a "leak check" version of the software, runs the tests and generates a report by comparing the list of leaked blocks with a previous run.
The check code is also very simple - just override new and delete and store the caller's address (e.g. via __builtin_return_address) inside each block (extending the block size by 8). At the end live objects are enumerated and each return address is resolved via debug symbols and printed out.
We also need to expose a heap walk as we have memory trackers on our malloc layer that adds tracking information (similar to the utrace structs) to each allocation so walking the heap would make jemalloc compatible with a big set of internal tools for us.
If anyone has tips on how to expose a heap walk I'm going to try to implement it for us internally and if successful I could do a pull request for it.
Activity
thestinger commentedon Feb 9, 2016
FWIW, there's a pending patch for this in Android:
https://android-review.googlesource.com/#/c/194202/
jasone commentedon Feb 9, 2016
I've implemented precise leak checking in multiple systems, and after long experience with them I no longer consider precise leak enumeration to be an important feature in malloc implementations. These mechanisms have a perfection-based false allure, but in practice they are extremely difficult to utilize.
How do you tell the difference between useful allocations and leaks? Unless you programmatically walk/track the reference graph to distinguish live/dead objects, it's infeasible to recognize leaks for non-toy applications. Okay, so what does work well? I know of three systems that are powerful and practical:
Regarding your criticisms of jemalloc's leak checking, i.e. heap profiling:
jeprofscript to post-process heap profiles, and the result is a detailed representation of call graphs associated with extant allocations.All that said, I'm not totally against enumeration mechanisms, and in fact I have other long term plans that might require this functionality (see GC-related discussion in #203). But, it's not a priority right now, and it's really tricky to provide an API that doesn't suffer from extreme fragility and bad interactions with allocator internals.
jasone commentedon Feb 9, 2016
Re: the Android patch, it's a bit odd that the application has to prime iteration with addresses. But, jemalloc doesn't currently track non-huge chunks, because it would only need such tracking for live object iteration.
thestinger commentedon Feb 9, 2016
In Android, it's being used for a leak detector based on a tracing garbage collector so it's the API needed for tracing through allocations.
rustyx commentedon Feb 9, 2016
Well, for us heap walk has been vital. We work with >20GB of tiny objects and when certain arenas do not go back to 0, we know that a leak has occurred, but we don't know where.
Of course global heap walk is imperfect as it gives many false positives, but it's quite easy to visually distinguish those from actual leaks, if one know from where in the code it was allocated (and especially if arena IDs are also reported).
Jemalloc's profiling with sampling set to collect every allocation slows down our process (which is already slow) by a factor of 8, so that's impractical to use.
We had a compile-time setting to add the location in the code where the allocation occurred to each block, and this performed relatively well. But without heap walk functionality we can't use this mechanism anymore.
So to be clear, jemalloc's profiling almost solves our problem, but is too slow. My request is therefore to either extend the profiling API with a heap walk API or to provide hooks so I can supply custom call stack info (which is where I think the biggest performance impact is). Note - we are on Windows, if that matters. And if it doesn't report the arena, then to add that as well (haven't seen arenas being reported, but might have missed it).
jasone commentedon Mar 7, 2016
@rustyx, I still don't understand why it's important in your use case to "sample" every allocation. In my experience, much lower sample rates suffice for detecting all nontrivial leaks, so I'm still wondering if you're making leak detection harder than it needs to be, or if there's something unusual about your operating environment.
rustyx commentedon Mar 7, 2016
I don't know if that's unusual but we have a combination of long-lived data cubes and short-lived requests in the same process. Data cubes are large and are updated just a few times a day. If there is a leak in there of just a single allocation (which we did have in the past), sampling would be highly inefficient in finding it. With a "sample" rate of 8, I would have to run my process 8 times to catch it, and even then there is no guarantee that I will catch it.
Sampling just does not work for us. Live object enumeration on the other hand works very well; an automated process builds a "leak check" version of the software, runs the tests and generates a report by comparing the list of leaked blocks with a previous run.
The check code is also very simple - just override
newanddeleteand store the caller's address (e.g. via__builtin_return_address) inside each block (extending the block size by 8). At the end live objects are enumerated and each return address is resolved via debug symbols and printed out.santagada commentedon Oct 26, 2018
We also need to expose a heap walk as we have memory trackers on our malloc layer that adds tracking information (similar to the utrace structs) to each allocation so walking the heap would make jemalloc compatible with a big set of internal tools for us.
If anyone has tips on how to expose a heap walk I'm going to try to implement it for us internally and if successful I could do a pull request for it.