很多普通开发者, 甚至库的作者, 没有意识到 flate 算法内存占用过高的问题, 一个压缩器, 占用内存超过了 640KB !!!

package main

import (
    "compress/flate"
    "unsafe"
)

func main() {
    writer, _ := flate.NewWriter(nil, flate.BestSpeed)
    println(unsafe.Sizeof(*writer))
}

// output: 656648

我测试了一下三个 websocket package, 10000 连接, 开启压缩, 10s 广播一次, 结果如下

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  18563 caster    20   0 1340732 225504   4000 S   0.0   5.6  16:15.86 gws-linux-amd64
  18542 caster    20   0 1469756 351172   4912 S   0.0   8.8  25:17.99 gorilla-linux-a
  30294 caster    20   0 4791548   1.7g    880 S   0.0  44.6   0:07.33 nhooyr-linux-am
  • github.com/lxzan/gws
  • github.com/gorilla/websocket
  • github.com/nhooyr/websocket
    nhooyr降低到开 5000 连接才能正常测试