We're experiencing a memory leak when using the websocket blob implementation in React Native, and can't quite locate the issue.
Using Xcode Instruments we can see that the issue most likely happens in the way the framework handles binary messages in RCTSRWebSocket.m or perhaps later in the call tree at RCTWebSocketModule.m
Anyone with Object-C Skills who are able to see why some memory (presumably alocated to the received messages) aren't being properly released ?
Link to Github issue
The issue can finally be resolved. After digging around in the implementation of WebSockets and especially the blobs, I found that all blobs will stay in memory as long as they aren't being directly closed.
This means that after you're done with the received data, you should close the Blob like this:
ws.onmessage = function (e) {
// Do whatever with the data through e.data.
const data = e.data;
// When you are done with the received data, you must close the Blob:
e.data.close();
};
Maybe it's me but it seems the frameData is copied? It's NSData so a reference type and it's not clear to me why a copy is needed? It is necessary because it's changed afterwards and you want the original to remain the same? Otherwise copying is completely unnecessary and perhaps this helps things a bit? If all the data it's reading is copied and perhaps retained somehow in the '_handleMessage' function it can lead to big memory allocations.
So my first attempts would be:
[self _handleMessage:frameData];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With