Skip to content

Instantly share code, notes, and snippets.

@jacobsa
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsa/59f9bbfa096fd3c595dc to your computer and use it in GitHub Desktop.
Save jacobsa/59f9bbfa096fd3c595dc to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"os"
"runtime"
"runtime/pprof"
)
var fSize = flag.Int64("size", 0, "Number of zeroes to read.")
func run() (err error) {
// Collect a detailed heap profile, writing it out when we're finished.
runtime.MemProfileRate = 1
profile, err := os.Create("/tmp/mem.pprof")
if err != nil {
return
}
defer profile.Close()
defer func() {
pprof.Lookup("heap").WriteTo(profile, 1)
}()
// Read some zeroes.
zero, err := os.Open("/dev/zero")
if err != nil {
return
}
defer zero.Close()
_, err = ioutil.ReadAll(io.LimitReader(zero, *fSize))
if err != nil {
return
}
// Make some other allocations.
for i := 0; i < 1<<10; i++ {
io.MultiWriter(ioutil.Discard, ioutil.Discard)
}
return
}
func main() {
flag.Parse()
// Run.
err := run()
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment