以更紧凑的方式分配5GB的RAM

huangapple go评论99阅读模式
英文:

Allocate 5 GB of RAM in a more compact way

问题

我刚刚将一些C/C++代码移植到Go语言,它是一个微服务。它运行得很好,甚至比C/C++还要快。但是我在内存方面遇到了问题。

当我的程序启动时,它会分配大约4.5GB的内存,并从磁盘中读取数据填充到内存中,同时在加载数据时进行处理。然后它会持续运行数天(希望是数月),从内存中提供请求的服务。不幸的是,在处理和放置数据到内存中完成后,Go语言仍然会额外分配3.5GB的内存。我没有进行任何释放操作,只有分配操作,我不认为我的程序在任何时候真正使用了8GB的内存,所以我认为Go语言只是获取了额外的内存,因为它“感觉到”我可能很快会需要更多的内存,但实际上我并不需要。

我了解到Go语言不允许释放未使用的内存并将其返回给系统。我希望在同一台机器上运行更多的服务,尽可能节省内存,所以浪费几乎与实际使用量相同的内存感觉不对。

那么,我该如何保持内存占用的紧凑性,避免Go语言分配那额外的3.5GB空闲内存呢?

英文:

I just ported some code from C/C++ to Go, it is a microservice. It works well, even faster than in C/C++. But I have a problem with memory.

When my program starts it will allocate about 4.5GB RAM and will fill it with data from disc also processing data while loading, then it will run for days(hopefully months) serving the requests from RAM. Unfortunately after the processing and placement of data in RAM is finished, extra 3.5 GB of RAM remains allocated by Go. I do not do any deallocations, only allocations, I do not think my program really uses 8 GB at any point, so I think Go just acquires extra RAM because it "feels" I might need more soon, but I will not.

I read that Go does not allow any functionality to deallocate unused RAM to return it to system. I want to run more services on the same machine, saving as much of RAM as possible, so wasting almost as much as I really use feels wrong.

So how do I keep the memory footprint compact avoiding those empty 3.5 GB being allocated by Go?

答案1

得分: 1

说到虚拟内存(参见Povilas Versockas的《Go内存管理》RSS vs. VSZ),不要尝试在Go 1.11中运行你的程序。

参见golang/go问题28114:“运行时:由1.11编译的程序分配了不合理数量的虚拟内存”。
还有在这里讨论

可能与以下相关:

可能的解决方法:golang/go问题28081

英文:

Speaking of virtual memory (See "Go Memory Management" from Povilas Versockas, and RSS vs. VSZ), don't try your program with Go 1.11.

See golang/go issue 28114: "runtime: programs compiled by 1.11 allocate an unreasonable amount of virtual memory".
Also discussed here.

Possibly related to:

  • CL 85888: runtime: remove non-reserved heap logic
  • issue 10460: runtime: 512GB memory limitation

Possible workaround: golang/go issue 28081

答案2

得分: 0

很可能这是Go语言使用的虚拟内存,而不是实际的已提交的内存页面。

DeferPanic博客文章:“理解Go语言的内存使用”(2014年)

英文:

Most likely that is virtual memory that Go is using, not actual committed pages of RAM.

DeferPanic blog post: "Understanding Go Lang Memory Usage" (2014)

huangapple
  • 本文由 发表于 2017年3月8日 19:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42670404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定