-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
FileTarget: Reset reusable MemoryStream when above max capacity #5548
Conversation
I like the idea of not exploding into huge memory-usage after handling a single crazy LogEvent. But I prefer just discarding crazy-large objects (Larger than ex. 128 KByte), instead of constantly trying to re-assign the original capacity (Similar to the internal NLog StringBuilderPool) |
We quite improved our research:
After that
It's mean that finally we got 6X overhead |
@snakefoot I understand your idea of avoiding logging large messages, but a 6x increase just sounds crazy 🤯 Let me remind you that this logic per FIleTarget, of which there can be many (more than 10 in our case) |
I'm not rejecting the idea of better memory-usage after handling crazy large LogEvent. Actually I would really like to make it better :) I'm just saying that your initial pull-request can be improved (Avoid constantly re-assigning initial Capacity), and have guided you towards looking at the internal NLog |
Notice when I say "discarding crazy large objects" then it doesn't mean "discarding large logevents", but that reusable-objects (StringBuilder / MemoryStream) that have grown crazy large should be discarded (start from fresh, similar to how it is done by NLog |
I think your idea close to RecyclableMemoryStream. What are you think about it? |
Like that NLog has no dependencies, so would prefer improving the existing
|
Notice when I say "similar to NLog StringBuilderPool", then it doesn't mean that |
Already started implementing Pool based stream 😅 |
Yes sorry about the confusion. I have tendency to expect people to be mind-readers, and whatever that is crystal clear to me, should also be crystal clear to others :) |
Please review BufferPool before i will start inroduce it. I would like to write a couple of tests, but it seems that writing tests on Pools is not very common |
I'm surprised that it is possible to build something that is faster than the standard MemoryStream. Need to test this myself. The object-pool in NLog has been about reusing the existing .NET building-blocks, and not trying to create our own to maintain. So I'm a little reluctant going down this road of building our own Stream-class. Notice I will be going on vacation now, so expect less feedback |
In this case, we will reuse existing buffers as long as we do not exceed the expected size. If exceeded, we will release excess memory. I think this will help us avoid endless memory growth due to one large message
|
No problem. Enjoy your vacation 🙏
|
@snakefoot hi, any news? What's the next? |
I have created #5561 that tries to improve the Large-Object-Heap-handling for NLog. Each FileTarget should now have max-memory-usage of 10 MByte. Thus 7 FileTargets = 70 MByte. Either you can enroll my suggestions in your pull-request, or I can merge my pull-request and close this one. |
I fill enroll your suggestions |
And do you approve of the new memory behavior, after handling a huge LogEvent? |
Summarizing: StringBuilder is initialized with a size of 40 KB. After exceeding the maximum size of 400 KB, capacity will be reset to 40 KB. |
I believe this is a strong step forward, and we will be able to improve the values in the future. So yes, I agree and approve new behavior. |
Notice that a single unicode char is 2 bytes. So your Stringbuilder byte-values has to be multiplied by 2.
Bur happy you approve, also like that the capacity reset now uses a memory size below 85 KByte to avoid the Large Object Heap (LOH) from the beginning.
|
Quality Gate passedIssues Measures |
@RomanSoloweow Thank you for the contribution, and effort to improve the NLog code base. |
When is the new version planned to be released? |
I'm guessing within a week or two. Trying to see if |
Thank you for your review and help in solving the problem! |
NLog v5.3.3 has been released: |
Implementation for old feature request #4558.
Context:
We have rest logging (we log all requests and responses) and sometimes size of response can reach 4 megabytes or more. We came across the fact that logging requires about 400 megabytes of RAM. Here's what we found:
After testing this fix, the amount of memory consumed was reduced by 5 times: 750 megabytes vs 150.
I think this fix will reduce memory consumption for users logging large messages. Those who log large messages constantly can increase capacity