for range time.Tick not cleaning up garbage?

polaris · · 526 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a simple loop:</p> <pre><code>for range time.Tick(jobConfig.Interval) { // Fetch data from database, do some mild processing, and write back to database. // The data can flow from Redis -&gt; Redis or Redis -&gt; Cassandra. } </code></pre> <p>After every jobConfig.Interval, the daemon RSS size is about the same size as the code + the size of data that it fetches.</p> <p>Have you guys noticed something similar? I wonder if this is database driver specific as well. I&#39;m using gocql and go-redis.</p> <hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>Yes, well at least Tick leaves behind garbage. Try NewTicker. From <a href="https://golang.org/pkg/time/#Tick">the docs</a>, </p> <pre><code>Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it &#34;leaks&#34;. </code></pre></pre>pinpinbo: <pre><p>Interesting, thanks for pointing out the docs!</p></pre>jammerlt: <pre><p>I am pretty sure it&#39;s the code in the body of the loop. Did you try without the body?</p></pre>pinpinbo: <pre><p>Yup, the same exact code is called separately in http handler somewhere else. If it leaks over there, the problem would be very noticeable.</p></pre>itsmontoya: <pre><p>And you&#39;re sure that you don&#39;t have any strange closures?</p></pre>tty5: <pre><p>http handler is running in a goroutine, so it&#39;s entirely possible it gets GCed even if you don&#39;t do 100% percect cleanup.</p> <p>To rule out time.Tick simply replace it with <code>for i:=0; i&lt;someVal; i++</code> and compare</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

526 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传