Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address memory usage issue in time.After() #18

Merged
merged 1 commit into from
Apr 29, 2019

Conversation

satta
Copy link
Contributor

@satta satta commented Apr 26, 2019

I observed a continuous increase in memory usage when making heavy use of the cache with a long global TTL set. My usage pattern was mostly setting and quickly deleting values again (manually), and my intention was to use the TTL only to purge corner cases which were not cleaned up by my own code.

Profiling revealed that the memory in use is mostly timers:

$ go tool pprof foo
[...]
Time: Apr 26, 2019 at 3:09pm (CEST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top20
Showing nodes accounting for 1181.16MB, 99.36% of 1188.80MB total
Dropped 11 nodes (cum <= 5.94MB)
Showing top 20 nodes out of 24
      flat  flat%   sum%        cum   cum%
  983.58MB 82.74% 82.74%  1034.31MB 87.00%  time.NewTimer
   50.73MB  4.27% 87.00%    50.73MB  4.27%  time.startTimer

[...]
----------------------------------------------------------+-------------
      flat  flat%   sum%        cum   cum%   calls calls% + context 	 	 
----------------------------------------------------------+-------------
                                         1034.31MB   100% |   github.com/ReneKroon/ttlcache.(*Cache).startExpirationProcessing
  983.58MB 82.74% 82.74%  1034.31MB 87.00%                | time.NewTimer
                                           50.73MB  4.90% |   time.startTimer
----------------------------------------------------------+-------------

This was likely to a common quirk with time.After(), see e.g. [1] and more.Some minor refactoring (see diff) resulted in the same behaviour but without excessive memory usage. Tests complete with no problem.

[1] https://medium.com/@oboturov/golang-time-after-is-not-garbage-collected-4cbc94740082

@ReneKroon
Copy link
Contributor

Thanks for reporting, i use the global TTL usually with a 2 minute lifetime so did not face the issue yet. I'm going to check and merge it.

@ReneKroon ReneKroon merged commit 86f5295 into jellydator:master Apr 29, 2019
@satta satta deleted the timer-leak branch April 29, 2019 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants