fgeller / kt

Kafka command line tool that likes JSON
MIT License
950 stars 100 forks source link

KT built for Win10/11 is possibly leaking memory #138

Open gfeyer opened 1 year ago

gfeyer commented 1 year ago

Hi guys!

Summary: I'm using KT extensively on macos/linux/windows and it works great for linux and mac. The issue is that the windows kt.exe build is leaking memory.

How to reproduce: in Win10/11, in main repo, do a standard go build. This generates kt.exe

in cmd prompt: kt.exe -brokers ip:port -offsets all=newest -topic topic_name

In task manager, monitor kt.exe and memory is increasing linearly, as-if all the data that is consumed from the topic is loaded into RAM and not released. It starts around 15MB and keeps on growing into the hundreds of MB

Notes: On Ubuntu Linux and macOS the RAM usage is stable at 26MB

Any thoughts on this? I compiled with latest go version and tested on both Windows10 and Windows11.

Thanks in advance for any insights! Best regards,

gfeyer commented 1 year ago

Adding more info: I've let this run until it reached about 200MB when the GC started to kick in with very large "stop the world" pauses. Consuming from the stream is stopping for 200-500ms at a time every couple of seconds, though the memory did start to go down. Waiting for a while it stabilizes around 100MB RAM but it has large increases in RAM every second putting a large strain on the GC to bring it back down and stopping the app every couple of seconds for the cleanup. Any ideas? Thanks in advance

gfeyer commented 1 year ago

I ran a profiler on KT and it's actually coming from the underlying sarama library. I'll continue to trace it in Sarama. Thanks. For future reference:

github.com/Shopify/sarama.(*Broker).responseReceiver
N:\go\pkg\mod\github.com\!shopify\sarama@v1.38.1\broker.go

  Total:     43.40MB    43.40MB (flat, cum) 84.37%
   1152            .          .                     dead = PacketDecodingError{fmt.Sprintf("correlation ID didn't match, wanted %d, got %d", response.correlationID, decodedHeader.correlationID)} 
   1153            .          .                     response.handle(nil, dead) 
   1154            .          .                     continue 
   1155            .          .                 } 
   1156            .          .            
   1157      43.40MB    43.40MB                 buf := make([]byte, decodedHeader.length-int32(headerLength)+4) 
   1158            .          .                 bytesReadBody, err := b.readFull(buf) 
   1159            .          .                 b.updateIncomingCommunicationMetrics(bytesReadHeader+bytesReadBody, requestLatency) 
   1160            .          .                 if err != nil { 
   1161            .          .                     dead = err 
   1162            .          .                     response.handle(nil, err) 
fgeller commented 1 year ago

sorry for the delay, but 🎉 that you figured it out :) let me know if it'd help to upgrade sarama or bring a fix in somehow