Limiting cpu/memory usage for a go program

blov · · 1088 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a program that could easily take every bit of power it can get to complete a very long running task. But that is not what I want, because it would affect the user, is there some way to limit if much cpu and memory the program can use?</p> <hr/>**评论:**<br/><br/>ghaering: <pre><p>You can do that with the help of your operating system. On Unix, you can limit the amount of memory a process can grab using &#34;ulimit&#34;, you can set the priority of the process using &#34;nice&#34; or you can go even more fancy using (Linux) cgroups.</p></pre>sybrandy: <pre><p>Along similar lines which may be easier (I never touched cgroups directly), you could run the program in a Docker container. We can restrict CPU shares and memory via marathon and I&#39;m pretty sure you can do it with just Docker as well.</p></pre>Zilog8: <pre><p>Well, one simple way to limit CPU use would be to set <a href="https://golang.org/pkg/runtime/#GOMAXPROCS">GOMAXPROCS</a> to less than the actual count of CPU cores, thereby leaving the remaining cores free for other programs.</p></pre>Hexodam: <pre><p>That is something I will probably use because the program will run on Windows as well </p></pre>chrj: <pre><p>You should leave that up to the OS. I usually run my go programs in systemd units. See the <code>Limit*</code> parameters of <a href="https://www.freedesktop.org/software/systemd/man/systemd.exec.html" rel="nofollow">systemd.exec</a>.</p></pre>BraveNewCurrency: <pre><blockquote> <p>because it would affect the user</p> </blockquote> <p>Can you elaborate? What, specifically are you trying to avoid? Computers are designed to run multiple programs at once, so programmers rarely worry about this sort of thing. (Well, except for tricks to use less RAM.)</p> <p>Linux has <a href="http://www.cyberciti.biz/faq/cpu-usage-limiter-for-linux/" rel="nofollow">several</a> <a href="https://unix.stackexchange.com/questions/44985/limit-memory-usage-for-a-single-linux-process#44988" rel="nofollow">ways</a> of limiting CPU and RAM usage. <a href="https://www.docker.com/" rel="nofollow">Docker</a> or <a href="https://linuxcontainers.org/" rel="nofollow">LXC</a> are the newest way to add limits.</p> <p>Note that you will need to put the whole program into this mode. In a normal language, you could set the CPU priority per-thread. But in Go, Goroutines aren&#39;t mapped to a single Thread. So it&#39;s all-or-nothing. You could shell out to a 2nd program that was limited.</p> <p>Another idea would be to insert &#34;breathing room&#34; in your program by doing sleep() for 10ms or 100ms at a time. This would let the computer get other work done. (I had to do this last month, when trying to delete millions of old rows out of a production database.)</p></pre>

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

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