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.
When we read off the read queue, we alias the slice. What has been forgotten though is to remove the reference to the entry in the slice after it has been read. This fixes that by overwriting the entry with a new Event. This now allows the GC to pick up the buffered data that the event was holding onto.
What?
This is removing the reference to the data that is no longer needed once the Event has been sent and read off the queue,. but before the read queue is aliased.
Why?
I remember having this complicated write/read slice in the emitter queue. The reason we needed this to keep the incoming CDP messages in order, otherwise they would go out of order (PR). The issue is that once the event is read off the read queue, the read slice is aliased, but the event is still held in memory in the underlying array!
By overwriting the entry before the alias the data that was being held onto is now free to be picked up by the GC.
How to Test
Testing this change requires the following to be performed:
main
, so checkoutmain
;mem-test.js
./mem_bench.sh on_main.csv mem-test.js
:Bash script to run and keep track of memory usage
go tool pprof -http=":8002" pprof-heap-1
inuse_space
already selected in the graph view, go tohttp://localhost:8002/ui/flamegraph
;fix/memory-leak-in-event-queue
;io.ReadAll
hasn't grown:Checklist
Related PR(s)/Issue(s)
Updates: #1480