Skip to content

Instantly share code, notes, and snippets.

@Code-Hex
Created December 6, 2020 06:44
Show Gist options
  • Select an option

  • Save Code-Hex/f7e0c88149575ad2ad9f5e617ee0f70f to your computer and use it in GitHub Desktop.

Select an option

Save Code-Hex/f7e0c88149575ad2ad9f5e617ee0f70f to your computer and use it in GitHub Desktop.
investigate memory usage
package main
/*
#cgo darwin CFLAGS: -x objective-c -fobjc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization.h"
*/
import "C"
import (
"fmt"
"runtime"
"sync"
)
const (
loopSize = 10000
sampleSize = 100
)
var (
allocs int
frees int
lock sync.Mutex
)
func main() {
fmt.Printf("sample\ttotal\tallocs\tmallocs\tfrees\theap\tac\tfc\n")
for sample := 1; sample <= sampleSize; sample++ {
for i := 0; i < loopSize; i++ {
a := NewAllocator(10 * 1024)
_ = a
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
lock.Lock()
fmt.Printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", sample,
m.TotalAlloc / 1024,
m.Alloc,
m.Mallocs,
m.Frees,
m.HeapAlloc,
allocs,
frees,
)
lock.Unlock()
}
}
func NewAllocator(size int) *Allocator {
a := &Allocator{}
a.alloc(size)
// runtime.SetFinalizer(a, free)
return a
}
type Allocator struct {
memory C.VZLinuxBootLoaderPtr
}
func (a *Allocator) alloc(size int) {
c := C.CString("/Users/codehex/Desktop/vmlinuz")
a.memory = C.newVZLinuxBootLoader(c)
lock.Lock()
defer lock.Unlock()
allocs++
}
func free(a *Allocator) {
// C.deallocVZLinuxBootLoader(a.memory)
lock.Lock()
defer lock.Unlock()
frees++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment