containers / containers/gvisor-tap-vsock

missing Close() method for VirtualNetwork

Open
#161 9 comments 0 reactions 1 assignee Claimed by @gbraad View on GitHub
Dominant language
Go
Stars
435
Forks
102
Avg merge
1d 23h
Merged PRs (30d)
14

Description

Currently, there's no way to free the memory allocated by VirtualNetwork.

For instance, the following code will leak around ~100MB of memory:

```go
config := types.Configuration{
Debug: false,
CaptureFile: "",
MTU: 1500,
Subnet: "10.10.10.0/16",
GatewayIP: "10.10.10.1",
GatewayMacAddress: "12:34:56:78:aa:bb",
DHCPStaticLeases: map[string]string{},
DNS: []types.Zone{},
Forwards: map[string]string{},
NAT: map[string]string{},
GatewayVirtualIPs: []string{"10.10.10.254"},
Protocol: types.QemuProtocol,
}

fmt.Println("Memory usage before creating virtual networks:")
memStats()

for i := 0; i < 1000; i++ {
_, err := virtualnetwork.New(&config)
if err != nil {
panic(err)
}
}

runtime.GC()

fmt.Println("Memory usage after creating virtual networks:")
memStats()
```

Output:
```
Memory usage before creating virtual networks:
Alloc = 0 MiB TotalAlloc = 0 MiB Sys = 10 MiB NumGC = 0
Memory usage after creating virtual networks:
Alloc = 97 MiB TotalAlloc = 125 MiB Sys = 410 MiB NumGC = 6
```

The implementation of memStats() (taken from [here](https://gist.github.com/j33ty/79e8b736141be19687f565ea4c6f4226)):

```golang
func memStats() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)

}

func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.