Skip to content

Instantly share code, notes, and snippets.

@anarcher
Created August 31, 2013 09:05
Show Gist options
  • Save anarcher/6397079 to your computer and use it in GitHub Desktop.
Save anarcher/6397079 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"runtime"
"time"
)
func makeBuffer() []byte {
return make([]byte, rand.Intn(5000000)+5000000)
}
func main() {
pool := make([][]byte, 20)
var m runtime.MemStats
makes := 0
for {
b := makeBuffer()
makes += 1
i := rand.Intn(len(pool))
pool[i] = b
time.Sleep(time.Second)
bytes := 0
for i := 0; i < len(pool); i++ {
if pool[i] != nil {
bytes += len(pool[i])
}
}
runtime.ReadMemStats(&m)
fmt.Printf("%d,%d,%d,%d,%d,%d\n", m.HeapSys, bytes, m.HeapAlloc,
m.HeapIdle, m.HeapReleased, makes)
}
}
package main
import (
"fmt"
"math/rand"
"runtime"
"time"
)
func makeBuffer() []byte {
return make([]byte, rand.Intn(5000000)+5000000)
}
func main() {
pool := make([][]byte, 20)
buffer := make(chan []byte, 5)
var m runtime.MemStats
makes := 0
for {
var b []byte
select {
case b = <-buffer:
default:
makes += 1
b = makeBuffer()
}
i := rand.Intn(len(pool))
if pool[i] != nil {
select {
case buffer <- pool[i]:
pool[i] = nil
default:
}
}
pool[i] = b
time.Sleep(time.Second)
bytes := 0
for i := 0; i < len(pool); i++ {
if pool[i] != nil {
bytes += len(pool[i])
}
}
runtime.ReadMemStats(&m)
fmt.Printf("%d,%d,%d,%d,%d,%d\n", m.HeapSys, bytes, m.HeapAlloc,
m.HeapIdle, m.HeapReleased, makes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment