-
Notifications
You must be signed in to change notification settings - Fork 962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TimeWindowMax#record causes allocation #3193
Comments
@shakuzen I can't remember exactly, but it seems to have been done that way as part of refactoring to reduce duplication. It'd be better to be reverted if it causes any performance downsides. |
I did quite a bit of investigation for this. It's a bit difficult to be certain about things because behavior can vary depending on the compiler and other factors. I do not see any garbage from the lambda in a heapdump, but I do see the same allocation you are seeing with the profiler. I think it's likely the allocation is happening on the stack due to escape analysis. If I add a breakpoint on the lambda and pause execution in it, then I can see garbage in a heapdump for the lambda instance. Since we don't need to use a capturing lambda here, and this is the hot path for recording, we should avoid the allocation. |
The capturing lambda that was used resulted in allocation on each invocation of the record method, which is on the hot path for metrics recording. To reduce the overhead of metrics, avoid the capturing lambda that results in allocation. Resolves micrometer-metricsgh-3193
@agusevas Thank you for reporting the issue. The fix is available in snapshots now (1.8.7-SNAPSHOT, 1.9.1-SNAPSHOT). I did not see the allocation in the memory profiler with these changes. Let us know if you still see allocation happening. |
@shakuzen Thank you for the fix. I have tested the 1.8.7-SNAPSHOT version locally. The allocation issue is fixed now. |
Some methods in
TimeWindowMax
class produce garbage because of usage of capturing lambdas. For example:Every time the
record(double sample, TimeUnit timeUnit)
is called, a new lambda instance will be created. See example memory allocation profiler stack trace:Environment
To Reproduce
How to reproduce the bug:
Run the test using memory allocation profiler. Sample result:
Expected behavior
Garbage creation is reduced in
TimeWindowMax
class.Additional context
See https://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood/ for more information about the capturing lambdas.
The text was updated successfully, but these errors were encountered: