O
O
onimor2021-11-01 11:06:29
C++ / C#
onimor, 2021-11-01 11:06:29

Where does such a large consumption of RAM come from?

Through gRPC I try to send a file to the server in parts:

private const int _maxSize = 3 * 1024 * 1024;
private byte[] _part = new byte[_maxSize];
private FileRequest _fileRequest;
 
 private async void UploadFiles(InputFileChangeEventArgs e)
 {
       
        foreach (var file in e.GetMultipleFiles())
        {
            try
            {
 
                using var call = GreeterClient.DownloadFile();
                ShowMessage($"Началась загрузка: {file.Name}",Severity.Normal);
                using (var ms = file.OpenReadStream(file.Size))
                {
 
                    while (ms.Length - ms.Position > 0)
                    {
                        if (ms.Length - ms.Position <= _maxSize)
                            _part = new byte[ms.Length - ms.Position];
 
                        await ms.ReadAsync(_part, 0, _part.Length);
                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        //отправка не сервер. Google.Protobuf.UnsafeByteOperations.UnsafeWrap Съедает оперативу. хотя он не копирует данные,
                        //в отличии от обычного метода Google.Protobuf.ByteString.CopyFrom, который съедает примерно так же...
                        _fileRequest = new FileRequest { FileName = file.Name, FileBytes = Google.Protobuf.UnsafeByteOperations.UnsafeWrap(_part) };
                        await call.RequestStream.WriteAsync(_fileRequest);
                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        _fileRequest = null; 
                        
                    }
                    await ms.DisposeAsync();
                }
            
                await call.RequestStream.CompleteAsync();
                var response = await call;
                var status = response.Status;
                ShowMessage(status,Severity.Success);
 
                call.Dispose();
            }
            catch(Exception ex)
            {
                ShowMessage(ex.Message, Severity.Error);
            }
            StateHasChanged();
        }
       
    }

If you leave the code that divides the file into parts, then it eats up a little more than the file itself. Although it should be 3mb.
When sent to the server, it eats up x10 of the file size (
I don’t understand why

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kachan, 2021-11-11
@MANAB

1. It is not clear at what point in time the problem is. The occupied RAM increased by 3MB AFTER sending the entire file, AT THE MOMENT of sending the file?
2. What eats up a little more than the file itself - so everything is correct, there is also memory that is spent on related code objects - the creation of a FileRequest, all sorts of wrappers inside other methods (as for UnsafeWrap). If, as a result of downloading the file, the memory occupied increased by 3 mb, it means that Garabge Collector has not yet cleared the _part
3 array. Why do you need to do

FileBytes = Google.Protobuf.UnsafeByteOperations.UnsafeWrap(_part)
? After all, so and so an array of bytes is sent? It seems that according to the description there, just a wrapper object is created for the array.
4 Well, the leak itself - here, at a slow speed, an array can be created several times. _part = new byte[ms.Length - ms.Position];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question