kakao / n2

TOROS N2 - lightweight approximate Nearest Neighbor library which runs fast even with large datasets
Apache License 2.0
567 stars 71 forks source link

Memory leaks in go bind #33

Closed muzzynine closed 3 years ago

muzzynine commented 4 years ago

Issue

I've seen memory increase due to n2.SearchByVector / n2.SearchById.

%memused, interval 1min
15.46
15.76
16.00
16.19 
16.34
16.49
16.66
16.84
16.99
17.14
17.29

https://github.com/kakao/n2/blob/0fe339177e8a22566d6958f924f8f0c382aab00c/bindings/go/n2gomodule.i#L36-L46

C++ object must need to be freed manually, because Go doesn't track C++ object ownership. Change the reference array of the slice header at the C++ level, but It is not enough to deallocate memory. Go garbage collector still doesn't know that object.

I added some typemap definition to release C++ object at the Go level.

Result

%memused, interval 1min
14.74
14.81
14.94
15.02
15.03
15.03
15.04
15.04
15.05
15.01
15.02