runtime包包含了一些对go的runtime系统相互作用的一系列操作,例如操作goroutines的函数。它还包括一些反射包用的低级类型信息(请查阅反射文档)。
1. Environment Variables
GOGC: 设置初始的垃圾回收百分比。默认值为GOGC=100;如果设置GOGC=off,则会完全关闭垃圾回收功能。runtime/debug package的SetGCPercent函数可以在运行时改变其值。
GOGCTRACE: 从垃圾回收处控制debug输出。
GOMAXPROCS : 控制同时运行用户态go程序的操作系统线程数。
GOTRACEBACK : 控制错误导致的输出数量。
GOARCH, GOOS, GOPATH, GOROOT: 386或amd64、linux或windows、开发目录、安装目录。
compiler.go debug.go error.go extern.go mem.go mgc0.go softfloat64.go type.go
2. 基本函数
func GC()
运行垃圾回收。
func NumCPU() int
返回本地主机的逻辑CPU数量。
func GOMAXPROCS(n int) int
设置可以同时进行运算的CPU数量;如果n<1,则不改变当前设置;可以通过上面的NumCPU函数获得参数n。
func Goexit()
终止调用它的goroutine;其它goroutine不受影响。
func Gosched()
调用此函数的goroutine会主动放弃处理器,让其它goroutine使用此处理其,最初的goroutine排队等待被执行。
func NumGoroutine() int
返回当前存在的goroutine数量。
3. type Func
type Func struct { // contains filtered or unexported fields }
func (f *Func) Entry() uintptr
返回函数的入口地址。
func (f *Func) FileLine(pc uintptr) (file string, line int)
根据程序的入口地址pc,返回文件名及源代码行数。
func (f *Func) Name() string
返回函数名。
4. type MemStats
type MemStats struct {
// General statistics.
Alloc uint64 // bytes allocated and still in use
TotalAlloc uint64 // bytes allocated (even if freed)
Sys uint64 // bytes obtained from system (should be sum of XxxSys below)
Lookups uint64 // number of pointer lookups
Mallocs uint64 // number of mallocs
Frees uint64 // number of frees
// Main allocation heap statistics.
HeapAlloc uint64 // bytes allocated and still in use
HeapSys uint64 // bytes obtained from system
HeapIdle uint64 // bytes in idle spans
HeapInuse uint64 // bytes in non-idle span
HeapReleased uint64 // bytes released to the OS
HeapObjects uint64 // total number of allocated objects
// Low-level fixed-size structure allocator statistics.
// Inuse is bytes used now.
// Sys is bytes obtained from system.
StackInuse uint64 // bootstrap stacks
StackSys uint64
MSpanInuse uint64 // mspan structures
MSpanSys uint64
MCacheInuse uint64 // mcache structures
MCacheSys uint64
BuckHashSys uint64 // profiling bucket hash table
// Garbage collector statistics.
NextGC uint64 // next run in HeapAlloc time (bytes)
LastGC uint64 // last run in absolute time (ns)
PauseTotalNs uint64
PauseNs [256]uint64 // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
NumGC uint32
EnableGC bool
DebugGC bool
// Per-size allocation statistics.
// 61 is NumSizeClasses in the C code.
BySize [61]struct {
Size uint32
Mallocs uint64
Frees uint64
}
}
记录所统计的内存分配情况。
原作者地址:https://my.oschina.net/renguijiayi/blog/159236