Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leak if sending data in a loop. #81

Open
forlayo opened this issue Jan 19, 2023 · 2 comments
Open

Leak if sending data in a loop. #81

forlayo opened this issue Jan 19, 2023 · 2 comments

Comments

@forlayo
Copy link

forlayo commented Jan 19, 2023

I am trying to check net capacity and I am doing:

    private async Task FakeDataSource()
    {
        await Task.Delay(500);
        Random rnd = new Random();
        var randomBytes = new byte[64000]; // 64k
        rnd.NextBytes(randomBytes);

        while (true)
        {
             var packet = new VideoPacket(1280, 720, new MemoryStream(randomBytes , 0, randomBytes .Length, false, true));
             _client.Send(packet);
        }
    }
  • _client is SimpleProtoClient as in the example.
  • VideoPacket is a message defined as
message VideoPacket
{
    uint16 width;
    uint16 height;
    bytes data;
}

Memory goes high in few seconds taking some Gb and then it crashes, I can see that an object of type Buffer has got all the RAM. As a weird note, If I add "await Task.Delay(1);" after the Send() it doesn't crash (but obviously is sending just a fraction of what's possible ).

What I am doing wrong ? How can I send as much as possible?

@chronoxor
Copy link
Owner

You should limit a send buffer frame to some reasonable value (e.g. 10mb per client) and control in your code with BytesPending property or void OnSent(long sent, long pending) handler.

Also it is possible to generate and send a first portion of data, and wait until virtual void OnEmpty() handler it called and all the data was sent, then generate another portion.

@ceceomer
Copy link

@forlayo can you put this line

var packet = new VideoPacket(1280, 720, new MemoryStream(randomBytes , 0, randomBytes .Length, false, true));

out of while loop?
Your code generate garbage in a loop. There is no point to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants