donglian7879 2018-04-06 14:00
浏览 81
已采纳

C代码是否喜欢Go GC的防碎片策略? [关闭]

Corrected the false implications:

Golang's GC does virtual address space defragmentation fragmentation-prevention strategies, which enables a program to run for a very long time (if not ever).

But it seems C code (cgo or SWIG) has no means of having it's memory pointers updated in case they get moved elsewhere. getting benefit from these strategies.

  1. Is it true? Won't C code get benefit from Golang's virtual address space defragmentation fragmentation-prevention, and will finally get fragmentation?

  2. If that's false, how?

  3. Also, what happens to any DLL code loaded by C code (e.g. Windows DLLs) ?

(The question is updated to correct my wrong assumptions)

  • 写回答

2条回答 默认 最新

  • douyan1882 2018-04-08 15:03
    关注

    I'm afraid you might be confusing things on multiple levels here.

    First, calling into C in a production-grade Go code is usually a no-go right from the start: it is slow; as slow as making a system call — as for the most part it really works as a system call: one need to switch from Go stack to C stack and have the OS thread which happened to be executing the Go code which made the cgo call to be locked to that thread even if something on the C side blocks.

    That is not to say you must avoid calling out to C, but this means you need to think this through up front and measure. May be setting up a pool of worker goroutines onto which to fan out the tasks which need to make C calls.

    Second, your memory concerns might be well unfounded; let me explain.

    Fragmenting virtual memory should be a non-issue on contemporary systems usually used to run Go programs (I mean amd64 and the like). That is pretty much because allocating virtual memory does not force the OS to actually allocate physical memory pages — the latter happens only when the virtual memory gets used (that is, accessed at an address happening to point into an allocated virtual memory region). So, want you or not, you do have that physical memory fragmentation problem anyway, and it is getting sorted out at the OS and CPU level using multiple-layered address translation tables (and TLB-caches).

    Third, you appear to be falling into a common trap of speculating about how things will perform under load instead of writing a highly simplified model program and inspecting how it behaves under the estimated production load. That is, you think a problem with allocating C memory will occur and then fancy the whole thing will not work.

    I would say your worries are unfounded — given the amount of production code written in C and C++ and working under hardcore loads.

    And finally, C and C++ programmers tred the pathways to high-performance memory management long time ago. A typical solution is using custom pool allocators for the objects which exhibit the most allocation/deallocation churn under the typical load. With this approach, the memory allocated on your C side is mostly stable for the lifetime of your program.

    TL;DR

    Write a model program, put the estimated load on it and see how it behaves. Then analyze, what the problems with the memory are, if any, and only then start attacking them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。