一种存储字节数组时减少内存使用的方法

问题描述 投票:-2回答:1

我现在是一个初学者,学习几天套接字

private void SendFile(Socket client, string user, string folder1, string folder2, string zipFile)
{
    byte[] zipBytes = File.ReadAllBytes(zipFile); //file

    string fileInfoStr = user + ","
   + folder1 + ","
   + folder2 + ","
   + Path.GetFileName(zipFile) + ","
   + zipBytes.Length;

    byte[] fileInfo = Encoding.UTF8.GetBytes(fileInfoStr);

    byte[] fileInfoLen = BitConverter.GetBytes(fileInfo.Length);
    var clientData = new byte[4 + fileInfo.Length + zipBytes.Length];

    fileInfoLen.CopyTo(clientData, 0);
    fileInfo.CopyTo(clientData, 4);
    zipBytes.CopyTo(clientData, 4 + fileInfo.Length);

    // Begin sending the data to the remote device.  
    client.BeginSend(clientData, 0, clientData.Length, 0,
new AsyncCallback(SendCallback), client);

    Receive(client);
}

例如,问题是我尝试发送一个大的zip文件(300mb),内存使用率高达600mb +。我被困在如何减少内存使用上。谢谢。

c#
1个回答
0
投票

您通过不使用内存来减少内存使用量。就像不使用这样的语句:

© www.soinside.com 2019 - 2024. All rights reserved.