Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How do you do this? I didn't know there was a way to temporarily disable the GC for specific blocks.


You can toggle the GC with https://golang.org/pkg/runtime/debug/#SetGCPercent. Turn it off before you enter your block and enable it again at the end of your block.


But can you guarantee that the GC actually makes progress that way?


Maybe you can, but maybe you shouldn't.

That is, you do this if you want a block to run without the GC interrupting it. That is, it's a really high-priority (realtime, or close) block. When it needs to run, it's the most important use of the CPU. But if you demand that the GC makes progress, you're saying that it also is important. And it is, but it's less important than the critical block. If you don't say that, then you wind up with "everything is important", and that way lies madness.

But you may be able to do it. You need enough CPU bandwidth that you can run your critical sections and spend enough time outside them that the garbage collector keeps up. (And then, every time you add to your code, you need to make sure that the CPU still has enough time...)


What does it mean for a GC to "make progress"? My understanding was that the OP wanted to stop the GC for a particular code block, so it would seem that any "progress making" would be undesirable.


https://golang.org/pkg/runtime/debug/#SetGCPercent

Do note that it triggers a GC when you call this, so it's not for all situations, but can be useful.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: