Skip to content

Instantly share code, notes, and snippets.

@chenchun
Last active July 30, 2024 08:00
Show Gist options
  • Save chenchun/20f02df39522db98186429f0a8b66c8e to your computer and use it in GitHub Desktop.
Save chenchun/20f02df39522db98186429f0a8b66c8e to your computer and use it in GitHub Desktop.
go cross compile, compile linux binary on mac osstatic compile, go memory cpu debug
go version < 1.5
cd /usr/local/go/src
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 ./make.bash --no-clean
all go version, compile your app
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -v .
env GOOS=linux GOARCH=amd64 go build -v .
#静态编译
#编译时设置 CGO_ENABLED=0 关闭cgo方式,-a强制重新编译,-ldflags '-s'通过删除一些debug信息,使得二进制文件更小
# CGO_ENABLED=0 go build -a -ldflags '-s' hello.go
# ldd ./hello
not a dynamic executable
# go mod
# go: verifying github.com/docker/[email protected]: checksum mismatch
# 如果你之前用 go 1.11.{3, 4} 之前的版本,现在又升级到 go 1.11.4 的话,你很可能需要重新生成一次你的 mod 缓存,否则就会报错 checksum mismatch
go clean -modcache
rm go.sum
go mod tidy
go get ./...
go env -w GOSUMDB=off
chenchun:temp (master) $ go mod tidy
go: github.com/containernetworking/[email protected]: parsing go.mod: missing module line
rm -rf ~/go/pkg/mod/*
# pprof document
# https://github.com/google/pprof/blob/main/doc/README.md
# https://software.intel.com/en-us/blogs/2014/05/10/debugging-performance-issues-in-go-programs
#memory
#The numbers in the beginning of each entry ("1: 262144 [4: 376832]") represent number of currently live objects, amount of memory occupied by live objects, total number of allocations and amount of memory occupied by all allocations, respectively.
curl http://myserver:6060/debug/pprof/heap?debug=1
go tool pprof -inuse_space http://localhost:2375/debug/pprof/heap
curl http://myserver:6060/debug/pprof/heap > heap.out
go tool pprof -http=:8080 heap.out
go tool pprof heap.out
# inuse_space — amount of memory allocated and not released yet
# inuse_object s— amount of objects allocated and not released yet
# alloc_space — total amount of memory allocated (regardless of released)
# alloc_objects — total amount of objects allocated (regardless of released)
top

Golang memory stats

/usr/local/go/src/runtime/mstats.go

Go divides the virtual address space of the heap into "spans", which are contiguous regions of memory 8K or larger. A span may be in one of three states:

  • idle, span contains no objects or other data, The physical memory backing an idle span can be released back to the OS (but the virtual address space never is), or it can be converted into an "in use" or "stack" span.

  • in use, contains at least one heap object and may have free space available to allocate more heap objects

  • stack, is used for goroutine stacks. Stack spans are not considered part of the heap. A span can change between heap and stack memory; it is never used for both simultaneously.

  • NextGC is the target heap size of the next GC cycle.

  • LastGC is the time the last garbage collection finished, as nanoseconds since 1970 (the UNIX epoch).

  • NumGC is the number of completed GC cycles.

  • NumForcedGC is the number of GC cycles that were forced by the application calling the GC function.

  • HeapAlloc is bytes of allocated heap objects. "Allocated" heap objects include all reachable objects, as well as unreachable objects that the garbage collector has not yet freed

  • HeapSys is bytes of heap memory obtained from the OS. HeapSys estimates the largest size the heap has had.

  • HeapIdle is bytes in idle (unused) spans.

  • HeapInuse is bytes in in-use spans.

  • HeapReleased is bytes of physical memory returned to the OS.

  • HeapObjects is the number of allocated heap objects.

  • StackInuse is bytes in stack spans.

  • StackSys is bytes of stack memory obtained from the OS.

HeapIdle-HeapReleased=the amounts of memeory can be returned to the OS

Go 将堆的虚拟地址空间划分为“跨度”,跨度是 8K 或更大的连续内存区域。跨度可能处于以下三种状态之一:

  • 空闲,跨度不包含任何对象或其他数据,支持空闲跨度的物理内存可以释放回操作系统(但虚拟地址空间永远不会释放),或者可以转换为“正在使用”或“堆栈”跨度。

  • 正在使用,包含至少一个堆对象,并且可能有可用空间来分配更多堆对象

  • 堆栈,用于 goroutine 堆栈。堆栈跨度不被视为堆的一部分。跨度可以在堆和堆栈内存之间切换;它永远不会同时用于两者。

  • HeapAlloc 是已分配堆对象的字节数。“已分配”堆对象包括所有可访问对象,以及垃圾收集器尚未释放的不可访问对象

  • HeapSys 是从操作系统获得的堆内存字节数。HeapSys 估计堆的最大大小。

  • HeapIdle 是空闲(未使用)跨度的字节数。HeapIdle-HeapReleased=可以返回到操作系统的内存量

  • HeapInuse 是正在使用跨度的字节数。

  • HeapReleased 是返回到操作系统的物理内存字节数。

  • HeapObjects 是已分配堆对象的数量。

  • StackInuse 是堆栈跨度的字节数。

  • StackSys 是从操作系统获得的堆栈内存字节数。

  • NextGC 是下一个 GC 周期的目标堆大小。

  • LastGC 是上次垃圾收集完成的时间,以纳秒为单位,自 1970 年(UNIX 纪元)以来。

  • NumGC 是已完成的 GC 周期数。

  • NumForcedGC 是应用程序调用 GC 函数强制执行的 GC 周期数。

#include <runtime.h>
void ·GetGoID(int32 ret) {
    ret = g->goid;
    USED(&ret);
}

package main
import (
    "fmt"
    "sync"
)
// Declaration is required
func GetGoID() int32
func main() {
    var wg sync.WaitGroup
    f := func() {
        wg.Add(1)
        go func() {
            fmt.Printf("goroutine %d\n", GetGoID())
            wg.Done()
        }()
    }
    for i := 0; i < 10; i++ {
        f()
    }
    wg.Wait()
}

$ go build
$ ./example
goroutine 20
goroutine 21
goroutine 22
goroutine 23
goroutine 24
goroutine 25
goroutine 26
goroutine 27
goroutine 28
goroutine 29
package main
import (
"fmt"
"sync"
)
func main() {
stop := make(chan struct{})
w := sync.WaitGroup{}
for i := 0; i < 3; i++ {
w.Add(1)
go func(stop <-chan struct{}) {
defer w.Done()
<-stop
fmt.Println("stoped")
}(stop)
}
close(stop)
w.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment