Standard library / runtime/debug

debug.SetGCPercent()

Sets the relative heap growth target for automatic GC and returns the prior value. A negative value disables ordinary automatic GC unless a memory limit requires it. Restore after experiments. Expected stdout: '75\n'.

Formal Syntax & Structure

go syntaxgo
func SetGCPercent(percent int) int

Parameters & Attributes

NameTypeDescription
percentintRelative heap growth target as a percentage; negative disables ordinary automatic GC.

Return Value

int

Previous GC percentage.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime/debug")
func main() {  old := debug.SetGCPercent(75)  defer debug.SetGCPercent(old)  fmt.Println(debug.SetGCPercent(75))}
Go Reference