New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
runtime: memory leak was observed in tcp client go program - RSS memory keeps growing #40404
Comments
Code : import (
"bufio"
"fmt"
"net"
"time"
)
func tcpThread() {
fmt.Println("Entered tcp Thread...")
conn, err := net.Dial("tcp", "127.0.0.1:8081")
if err != nil {
fmt.Println(err)
return
}
for {
Msg := "This is just a message"
fmt.Fprintf(conn, Msg+"\n")
fmt.Println("Message was sent to tcp server...")
message, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Print("Message from tcp server: " + message)
time.Sleep(10 * time.Second)
}
}
func main() {
fmt.Println("Entered into main")
go tcpThread()
select {}
} |
I have just written in simple TCP client and it would send message to TCP server every 10 sec |
I doubt this is related to the problem, but the statement |
What does the TCP server do? |
Unrelated
rather than starting a goroutine then going to sleep; just call |
TCP Server Code, import ( // only needed below for sample processing func main() {
} |
@santhoshkarthi thank you for the code sample. Before we continue can you please check all error values in your sample code and address the issue @ianlancetaylor highlighted earlier, #40404 (comment) |
@davecheney sure I will check and let you know, Thanks for your support |
TCP_client.go`package main import ( func tcpThread() { func main() { |
@davecheney @ianlancetaylor I removed below line, |
Do I need to flush STDOUT buffer? |
No, stdout is not buffered in Go |
Thank you for your sample code. You called it tcp server but there is no server there, only a client which connects to 127.0.0.1:8081 and sends it a short string every 10 seconds. |
Sorry @davecheney Yeah that is tcp client code, Now i dont see frequently increasing but slowly increasing after removing below line, but tcp_server RSS memory is increasing quickly(every 1 sec increasing 4 bytes) |
TCP_Server code, import ( // only needed below for sample processing func main() {
} |
Can you please post the version of the code with the error handling changes I suggested as well as addressing this line
Constructing a new bufio.Reader on each iteration is not correct. |
Are you asking for tcp_server or tcp_client or both? |
This code, #40404 (comment) |
@davecheney if i move "message, err := bufio.NewReader(conn).ReadString('\n')" outside for loop, it is printing "fmt.Print("Message Received: ", string(message))" with last received message continuously |
That is expected. The mistake is you should not construct a NewRedeader inside the loop, construct it outside the loop then call ReadString inside the loop to read a line at a time. Or consider using something like bufio.NewScanner which is the better choice to read lines at a time. |
TCP_server code ` package main import ( // only needed below for sample processing func main() {
} |
This is what you are asking me to do right @davecheney ? |
Please review #40404 (comment) and confirm if the original problem is fixed. |
sure @davecheney I will check that, Thanks |
@davecheney yeah now it is somewhat okay but still it is slowly increasing, |
where as tcp_client was stable |
New TCP server code, based on your suggestion, import ( // only needed below for sample processing func main() {
} ` |
I believe those values are in kilobytes, so the process grew by 32 kilobytes in 5 minutes. Does it grow steadily, or are the jumps quantised? Does the growth continue if you leave the process? If you export |
yeah it grows steadily, after I left the process ideal, now it grew up to 2164 KB from 1888KB, approx 300KB in 20 mins |
Attaching tcp_server code for your reference,`package main import ( // only needed below for sample processing func main() {
} |
Is this because I am creating new variable "msg, and err" everytime, |
I don’t think so, once that variable falls out of scope it’s storage should be freed by the garbage collector. The gctrace data I asked you to collect would confirm it. Do you have that data? |
@davecheney I am running GO program in imx6ul(ARMv7) embedded board, It does not have go compiler installed, I am just cross compiling go code in my Host PC and running it in my target. So is it possible to export "GODEBUG=gctrace=1" in the target |
yes, those variables affect the binary at run time, https://godoc.org/runtime |
So I just add this line into my go code, **"GODEBUG=gctrace=1"**Would that work sir? |
This is an environment variable, export it in the same shell as you run the program. |
@davecheney , yeah I have exported it in my target linux machine, where would i get the expected result/output? |
It will be printed to stderr as the program runs. Consult the documentation for more detail, https://godoc.org/runtime. |
@davecheney Program is running for around 20 mins, and RAM memory was grew up from 4536KB to 4720KB but still there is no garbage collected message printed in the console, like "gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P" |
It it likely that you did not correctly set the environment variable. |
"export GODEBUG=gctrace=1" this is what i did |
Can you show what you did? A screenshot or a copy an paste of the terminal session if possible? |
root@ccimx6ulsbc:/home/root# export GODEBUG=gctrace=1 |
Can you should me more? What happened after you set the environment variable and ran the program? |
after I set the variable and ran the program nothing was printed, I think i have mentioned already that my Embedded board does not have GO compiler installed, I am just cross compiling in host machine and running the binary in my target, Would it still work? |
Yes, this is supported. To assist you we need to see more than one line. Please review #40404 (comment) |
Based on the discussion in #40448 I am closing this as a duplicate. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?set GO111MODULE=
set GOARCH=arm
set GOBIN=
set GOCACHE=C:\Users\irhpoq\AppData\Local\go-build
set GOENV=C:\Users\irhpoq\AppData\Roaming\go\env
set GOEXE=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=linux
set GOPATH=D:\Karthik\Skills\Golang\GolangTraining-master
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set GOARM=7
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-fPIC -marm -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\irhpoq\AppData\Local\Temp\go-build269194878=/tmp/go-build -gno-record-gcc-switches
$ go env
What did you do?
What did you expect to see?
I see RSS memory (Physical RAM) memory was keep growing in the "top" in linux
What did you see instead?
It should not grow
The text was updated successfully, but these errors were encountered: