Fix memory leak in HkdfStreamingPrf #52
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Repetitive calls to
prf.compute()cause a memory leak. For example the following toy app will drain memory quickly:this is because in
HkdfInputStreamclass, the allocated ByteBuffer is adirectbuffer vs. aheap buffer. Direct buffers are allocated "by calling through to native OS allocs, bypassing the standard JVM heap... memory-storage areas of direct buffers are not subject to garbage collection because they are outside the standard JVM heap." See here for more info.Now the question might be the capacity is set to zero (
buffer = ByteBuffer.allocateDirect(0);) however the overhead involved the pointer itself adds up on MANY calls. This basically means repetitive calls toprf.computeare same as this toy app:Which sure enough will drain memory in matter of seconds. In practice since prf compute takes more time and is not called in such aggressive for loops; the leak is a lot slower and harder to notice. In our application, we had many repetitive calls resulting in an eventual drainage of machine memory. I have run these apps under a memory profiler and have confirmed the same findings. Stacktrace:
The fix is simply to swap
allocateDirectwithallocatewhich is heap managed and garbage collected. I've verified the fix does indeed solve the leak.