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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用