blob: 804ee7b0874ff24b370427a405731b8c2f941355 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# automemlimit
[](https://pkg.go.dev/github.com/KimMachineGun/automemlimit)
[](https://goreportcard.com/report/github.com/KimMachineGun/automemlimit)
[](https://github.com/KimMachineGun/automemlimit/actions/workflows/test.yml)
Automatically set `GOMEMLIMIT` to match Linux [cgroups(7)](https://man7.org/linux/man-pages/man7/cgroups.7.html) memory limit.
See more details about `GOMEMLIMIT` [here](https://tip.golang.org/doc/gc-guide#Memory_limit).
## Installation
```shell
go get github.com/KimMachineGun/automemlimit@latest
```
## Usage
```go
package main
// By default, it sets `GOMEMLIMIT` to 90% of cgroup's memory limit.
// You can find more details of its behavior from the doc comment of memlimit.SetGoMemLimitWithEnv.
import _ "github.com/KimMachineGun/automemlimit"
```
or
```go
package main
import "github.com/KimMachineGun/automemlimit/memlimit"
func init() {
memlimit.SetGoMemLimitWithEnv()
memlimit.SetGoMemLimit(0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroup, 0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupV1, 0.9)
memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupV2, 0.9)
}
```
|