Go Language Memory Management and Garbage Collection: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
=Internal=
=Internal=
* [[Go_Runtime#Memory_Management_and_Garbage_Collection|Go Runtime]]
* [[Go_Language#Memory_Management_and_Garbage_Collection|Go Language]]
* [[Go_Language#Memory_Management_and_Garbage_Collection|Go Language]]


Line 13: Line 14:
Aslo see: {{Internal|Go_Language#Variable_Deallocation|Variables}}
Aslo see: {{Internal|Go_Language#Variable_Deallocation|Variables}}
=Garbage Collection=
=Garbage Collection=
Garbage collector is a subsystem of the [[Programming Languages Concepts#Interpreter|interpreter]] that handles de-allocation of memory that is not needed by the program anymore. Garbage collectors can naturally assist interpreted languages, because there is an interpreter. Go is a compiled language, but is different in this respect: it does have garbage collection built into it, even if it does not come with an interpreter.
The Go compiler decides where a variable is allocated: on the [[#Stack|stack]] or on the [[#Heap|heap]], and it will be garbage-collected correctly.
GC can be invoked programmatically:
<syntaxhighlight lang='go'>
runtime.GC()
</syntaxhighlight>
As of Go 1.8, garbage collection pauses are generally between 10 and 100 microseconds.
=API=
<syntaxhighlight lang='go'>
var s runtime.MemStats
runtime.ReadMemStats(&s)
fmt.Printf("%v\n", s.Sys)
</syntaxhighlight>

Latest revision as of 20:44, 16 January 2024

External

Internal

TODO

To deplete, merge and delete this: Go Concepts - Memory Model

Overview

Stack

Heap

Variable Deallocation

Aslo see:

Variables

Garbage Collection

Garbage collector is a subsystem of the interpreter that handles de-allocation of memory that is not needed by the program anymore. Garbage collectors can naturally assist interpreted languages, because there is an interpreter. Go is a compiled language, but is different in this respect: it does have garbage collection built into it, even if it does not come with an interpreter.

The Go compiler decides where a variable is allocated: on the stack or on the heap, and it will be garbage-collected correctly.

GC can be invoked programmatically:

runtime.GC()

As of Go 1.8, garbage collection pauses are generally between 10 and 100 microseconds.

API

var s runtime.MemStats
runtime.ReadMemStats(&s)
fmt.Printf("%v\n", s.Sys)