I’ve been testing Go’s net package for managing 20,000 active connections. Initially, everything seems to work fine as memory usage stays around 150MB. However, when I disconnect all connections and try to reconnect them, the memory usage skyrockets to around 10GB and doesn’t seem to decrease.
Here’s a sample:
const (
port string = ":8080"
maxClients uint64 = 20000
bufSize = 1024 * 1024 * 1 // 1MB buffer size
)
func handleConnection(conn net.Conn, wg *sync.WaitGroup) {
defer wg.Done()
defer conn.Close()
buf := make([]byte, bufSize)
for {
n, err := conn.Read(buf)
if err != nil {
return
}
counter += 1
bufferSize += int64(n)
_, err = conn.Write(buf[:n])
if err != nil {
fmt.Println("Error writing:", err.Error())
return
}
}
}
func main() {
listener, err := net.Listen("tcp", port)
if err != nil {
fmt.Println("Error starting server:", err.Error())
return
}
defer listener.Close()
fmt.Println("Server started on port", port)
var wg sync.WaitGroup
sem := make(chan struct{}, maxClients)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err.Error())
continue
}
sem <- struct{}{}
wg.Add(1)
go func() {
defer func() { <-sem }()
handleConnection(conn, &wg)
}()
}
wg.Wait()
}
I tried diffrent GOGC env value, didn’t help