0

I'm trying to track down a memory leak that I think has to do with how I am using MS Bond. Specifically, the issue is likely on the subscriber side due to 'new' ArraySegment and InputBuffer objects being generated on every iteration inside a while loop.

On the publisher side, the code roughly looks as follows and I don't think there is a problem here:

open ZeroMQ
open Bond
open Bond.Protocols
open Bond.IO.Unsafe

let bond = new BondStructs.SomeStruct()
let output = new OutputBuffer()
let writer = new CompactBinaryWriter<OutputBuffer>(output)

let ctx = new ZContext()
let sock = new ZSocket(ctx, ZSocketType.PUB)
sock.Bind "tcp://localhost:12345"

while true do

    // do work, populate bond structure
    Marshal.To(writer, bond)
    use frame = new ZFrame(output.Data.Array, output.Data.Offset, output.Data.Count)
    sock.Send frame
    output.position <- 0L

The issue I think is on the subscriber side due to the fact the new ArraySegment and InputBuffer objects are being generated on every iteration and somehow the GC is unable to properly clean up.

open ZeroMQ
open Bond
open Bond.Protocols
open Bond.IO.Unsafe

let sock = new ZSocket(ctx, ZSocketType.SUB)
sock.SubscribeAll()
sock.SetOption(ZSocketOption.CONFLATE, 1) |> ignore
sock.Connect("tcp://localhost:12345")

while true do
    let zf = sock.ReceiveFrame()
    let segment = new ArraySegment<byte>(zf.Read())
    let input = new InputBuffer(segment)
    let msg = Unmarshal<Record>.From(input)
    zf.Close()

    // do work

Is there a way for me to push ArraySegment and InputBuffer lines above the while loop and reuse those objects within the loop?

1 Answers1

1

If the resulting msg instances are stored anywhere after processing the one request, that can keep the buffer alive.

The msg instances can have references to the underlying InputBuffer or ArraySegment when bonded- or blob-type fields are used.

Failing that, I've had luck with the WinDBG extension command !gcroot to figure out what's keeping something alive longer than I expected. If !gcroot provides more insights, but not a solution, please edit those details into the question.

chwarr
  • 6,777
  • 1
  • 30
  • 57