Hi, this should be fine, as long as you don't keep accumulating references to instances of any of those objects somewhere indefinitely, preventing GC from claiming them.
Have you tried running the application in debugger and taking a memory usage snapshot to find out what kind of objects are eating up your memory?
To make sure there is no obviously wrong behavior in Rebex WebSocket, I wrote the code below (where the server is just a simple echo server that sends back whatever it receives). I let it run the loop one million times, but did not observe an increase in memory usage over time.
var ws = new WebSocketClient();
ws.Connect(echoWebSocketServerWss);
var cancellation = new CancellationTokenSource();
string message = "hello".PadRight(16384, '!');
int runs = 0;
while (true)
{
await ws.SendAsync(message);
var buffer = new ArraySegment<byte>(new byte[8192]);
WebSocketReceiveResult result;
using (var ms = new MemoryStream())
{
do
{
result = await ws.ReceiveAsync(buffer, cancellation.Token);
ms.Write(buffer.Array, buffer.Offset, result.Count);
} while (!result.EndOfMessage);
runs++;
if ((runs % 1000) == 0) Console.WriteLine(runs);
}
}
I then tried a different code, where a new instance of WebSocketClient and connection is created during each iteration:
int runs = 0;
while (true)
{
var ws = new WebSocketClient();
ws.Connect(echoWebSocketServerWss);
var cancellation = new CancellationTokenSource();
string message = "hello".PadRight(16384, '!');
await ws.SendAsync(message);
var buffer = new ArraySegment<byte>(new byte[8192]);
WebSocketReceiveResult result;
using (var ms = new MemoryStream())
{
do
{
result = await ws.ReceiveAsync(buffer, cancellation.Token);
ms.Write(buffer.Array, buffer.Offset, result.Count);
} while (!result.EndOfMessage);
}
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "Bye.", cancellation.Token);
runs++;
if ((runs % 1000) == 0) Console.WriteLine(runs);
}
Expectedly, the memory usage was a bit higher, and each iteration took noticably longer due to repeated TLS negotiation, but again, memory usage kept hovering at the same level, and did not increase over time. I let this run 100 000 times.
I used .NET Framework 4.6-4.8 build of Rebex WebSocket R6.8 on Windows 11.