google / btree

BTree provides a simple, ordered, in-memory data structure for Go programs.
Apache License 2.0
3.93k stars 417 forks source link

Fix memory leak in removeAt methods. #9

Closed keep94 closed 8 years ago

keep94 commented 8 years ago

This change sets the last item in the slice to nil before the slice length is decreased when removing arbitrary elements. In the writeup below vertical bars denote that beginning and end of the visible part of the slice. Items beyond the last vertical bar denote items that lie beyond the end of the slice yet are still within the slice's capacity and therefore cannot be garbage collected.

Before this change, deleting B from | A B C D E | yielded | A C D E | E Then deleting E yielded | A C D | nil E. Even though E is deleted, E can't be garbage collected because a copy of it still sits beyond the end of the slice yet within the slice's capacity.

With this change, deleting B from | A B C D E | yields | A C D E | nil so that no extra copies of items or child pointers exist beyond the end of the slice allowing them to be GCed.

keep94 commented 8 years ago

Ping.

gconnell commented 8 years ago

Thanks!