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

time: document that time.Tick causes a resource leak #11662

Closed
ncw opened this issue Jul 10, 2015 · 0 comments
Closed

time: document that time.Tick causes a resource leak #11662

ncw opened this issue Jul 10, 2015 · 0 comments

Comments

@ncw
Copy link
Contributor

ncw commented Jul 10, 2015

Calling time.Tick causes a Ticker to leak because there is no interface to Stop() it.

Here is a demonstration which starts 100,000 go routines each of which calls time.Tick then closes the go routines down again in an orderly fashion

http://play.golang.org/p/zy5X4hAq0I

It sleeps at the end so you can run top or equivalent and see that it will be using 100% of a CPU core keeping the Tickers up to date even though the user no longer has access to them.

This is unfortunate but it was decided in #8001 that it was too difficicult to Stop the Tickers when they are garbage collected.

The documentation for NewTicker got changed after this, but not the documentation for Tick.

It currently reads

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. Useful for clients that have no need to shut down the ticker.

It doesn't say why you might want to shut the Ticker down.

My suggestion for a re-word is this

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. Note that this leaks associated resources; use NewTicker and Stop to avoid that.

That is possibly too alarmist, but I wanted the docs to note clearly that using time.Tick is leaking something which can't be recovered.

@ianlancetaylor ianlancetaylor added this to the Go1.5Maybe milestone Jul 10, 2015
@mikioh mikioh modified the milestones: Go1.5, Go1.5Maybe Jul 14, 2015
@golang golang locked and limited conversation to collaborators Jul 13, 2016
@rsc rsc unassigned robpike Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants