Description
What version of Go are you using (go version
)?
$go version go version go1.19.3 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env
)?
GOHOSTOS="linux" which is on Ubuntu 20.04, also tried it on MacOS.
go env
Output
$ go env GO111MODULE="auto" GOARCH="amd64" GOBIN="" GOCACHE="/home/kitarp29/.cache/go-build" GOENV="/home/kitarp29/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/kitarp29/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/kitarp29/go/" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19.3" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1140511453=/tmp/go-build -gno-record-gcc-switches"
What did you do?
This was an issue I ran into while at work this week. We are building a service on Golang and compressing the data before I save it to the DB. We tried using compress/gzip.
Implemented the basic way to compress the data using the NewWriter Function : Here. Problem is that when we tried this solution at scale(1 million entries at a time) there was a memory leak. We did Lfush and close all the writers and buffers we used. Still, it continued, verified using pprof.
We made an in-house implementation to solve the issue. We used new(gzip.Writer)
to create a Writer for us.
Tried like the last implementation, but to our surprise, it did not compress the string at all. In fact, the compressed data was larger than the original!
There was no compilation error whatsoever. Reason it increased was as the file/data is compressed an extra footer and added is added. After going through the code of gzip: Here
I realized that when we use gzip.NewWriter()
we are expected to pass an io.writer. This writer replaces the writer in the struct of gzip.Writer. I am assuming only then does it compresses the file.
Even I am not sure if it is BUG or not. I just wanted to share this and reduce the pain for someone else using this pkg.
What did you expect to see?
The pkg to compress the data provided. But it did not.
What did you see instead?
An uncompressed, even increased data than original