简体   繁体   English

如何在本地系统中运行 golang 应用程序时设置 memory 限制

[英]How to set memory limit while running golang application in local system

i want set heap size for go application in my windows machine我想在我的 windows 机器中为 go 应用程序设置堆大小

In java we used to provide -Xms settings as vm arguments in intellij but how to provide similar setting in golang and set memory limit for the go application. In java we used to provide -Xms settings as vm arguments in intellij but how to provide similar setting in golang and set memory limit for the go application.

Tried with试过了

<env name="GOMEMLIMIT" value="2750MiB" />

but not working但不工作

we are using go 1.6.2 version.我们使用的是 go 1.6.2 版本。

Go 1.19 adds support for a soft memory limit: Go 1.19 增加了对软 memory 限制的支持:

The runtime now includes support for a soft memory limit.运行时现在包括对软 memory 限制的支持。 This memory limit includes the Go heap and all other memory managed by the runtime, and excludes external memory sources such as mappings of the binary itself, memory managed in other languages, and memory held by the operating system on behalf of the Go program. This memory limit includes the Go heap and all other memory managed by the runtime, and excludes external memory sources such as mappings of the binary itself, memory managed in other languages, and memory held by the operating system on behalf of the Go program. This limit may be managed via runtime/debug.SetMemoryLimit or the equivalent GOMEMLIMIT environment variable.此限制可以通过runtime/debug.SetMemoryLimit或等效的GOMEMLIMIT环境变量进行管理。

You can't set a hard limit as that would make your app malfunction if it would need more memory.您不能设置硬限制,因为如果需要更多 memory,这会使您的应用出现故障。

To set a soft limit from your app, simply use:要从您的应用设置软限制,只需使用:

debug.SetMemoryLimit(2750 * 1 << 20) // 2750 MB

To set a soft limit outside of your app, use the GOMEMLIMIT env var, eg:要在您的应用程序之外设置软限制,请使用GOMEMLIMIT ,例如:

GOMEMLIMIT=2750MiB

But please note that doing so may make your app's performance worse as it may enforce more frequent garbage collection and return memory to OS more aggressively even if your app will need it again.但请注意,这样做可能会使您的应用程序的性能变差,因为它可能会强制执行更频繁的垃圾收集并将 memory 更积极地返回给操作系统,即使您的应用程序将再次需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM