Arman92 / go-tdlib

Golang Telegram TdLib JSON bindings
GNU General Public License v3.0
435 stars 101 forks source link

fix memory leak #74

Closed Juliia-b closed 3 years ago

Juliia-b commented 3 years ago

A memory leak was detected during load testing and application profiling. All screenshots were taken at approximately the same time (about 20 minutes from the application start).

image This screenshot shows the memory allocation for the SendAndCatch method, where flat = 557 MB and cum = 773 MB. A memory leak occurs due to the removal of channel waiter from the waiters map only if the time for processing the request expires (timeout error). Thus, any successfully completed request will not cause the channel to be removed from the map. This leads to a noticeable increase in the size of the map up to killing the process due to lack of memory.

image After fixing the indicated problem, cum fluctuates from 3 to 10 MB.

Also, in my opinion, using sync.Map in this library is inefficient and unnecessary. The documentation says: "The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex. " As far as I understand, both of the above cases are not used in the library.

Signed-off-by: Julia Bezrukova julika7391@gmail.com

Arman92 commented 3 years ago

@all-contributors please add @Juliia-b for fixing memory leak bug.

allcontributors[bot] commented 3 years ago

@Arman92

I've put up a pull request to add @Juliia-b! :tada: