jkramber
jkramber

Reputation: 21

Dart Websocket memory leak

I am using websockets to receive protcol buffers and experiencing a memory leak. This leak occurs regardless of incoming buffer size and frequency.

The protobufs are being received as Blobs but the same leak was present when receiving as an arrayBuffer. Currently all I have implemented is a packet handler that sets the Blob to null to attempt to invoke garbage collection.

My listen call: ws.onMessage.listen(handlePacket);

My event handler: void handlePacket(message) { message = null; }

I don't fully understand if the Stream of messageEvents in the websocket is a queue that is not dequeuing processed events, but it appears that all the memory allocated for the incoming events fails to be garbage collected. All help is appreciated.

EDIT Client side code:

  void _openSocket() {
    if (ws == null) {
      ws = new WebSocket('ws://localhost:8080/api/ws/open');
      // ws.binaryType = "arraybuffer";
    }
  }

  void _closeSocket() {
    if (ws != null) {
      ws.close();
      print("socket closed");
      ws = null;
    }
  }

void _openStream (String fieldName, [_]) {
  //Check if we need to open the socket
  _openSocket();
  //Request the proper data
  Map ask = {"Request": "Stream", "Field": fieldName};
  if (ws.readyState == 0){
    ws.onOpen.listen((_) {
      ws.send(JSON.encode(ask));
    });
  } else {
    ws.send(JSON.encode(ask));
  }

  activeQuantities++;
  if (activeQuantities == 1) {
    _listen();
  }
}

// Receive data from the socket
 _listen() {
  ws.onError.listen((_){
    print("Error");
  });
  ws.onClose.listen((_){
    print("Close");
  });
  ws.onMessage.listen(handlePacket);    
}

void handlePacket(message) {
  message = null;
}

Upvotes: 1

Views: 385

Answers (1)

jkramber
jkramber

Reputation: 21

Looks like Dartium is expected to leak memory, but when using Dart2js and running in Chrome it did manage to GC, albeit after showing the same symptoms as in Dartium. https://github.com/dart-lang/sdk/issues/26660

Upvotes: 1

Related Questions