Standard library / runtime/debug

debug.SetMemoryLimit()

Sets a soft runtime-managed memory limit in bytes; negative input queries the current setting. It does not cap all process memory or guarantee an exact RSS ceiling. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func SetMemoryLimit(limit int64) int64

Parameters & Attributes

NameTypeDescription
limitint64Soft runtime-managed memory limit in bytes; negative queries the current limit.

Return Value

int64

Previous soft memory limit in bytes.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime/debug")
func main() {  old := debug.SetMemoryLimit(512 << 20)  defer debug.SetMemoryLimit(old)  fmt.Println(debug.SetMemoryLimit(-1) == 512<<20)}
Go Reference