【问题标题】:Downloading blob results in more memory being used than size of actual blob下载 blob 导致使用的内存多于实际 blob 的大小
【发布时间】:2022-08-03 20:42:40
【问题描述】:

我试图下载一个大小约为 350mb 的 blob。当我使用 blob sdk 时,它占用了 2gb 的内存。谁能告诉我为什么会这样?

        var blobclient = blobContainerClient.GetBlobClient(blobName);

        using (var stream = new MemoryStream())
        {
            await blobclient.DownloadToAsync(stream);
            using (var streamReader = new StreamReader(stream))
            {
                var result = await streamReader.ReadToEndAsync();
                
            }
        }
  • 你是如何测量这个的?
  • 如果 blob 为 350 MB,则数组也将占用 350 MB——至少,因为在内部它可能会因动态分配而变大。然后最重要的是,您使用 StreamReader 生成的字符串将占用另外 700 MB 左右,假设 blob 主要是 ASCII 数据,因为字符串在内部是 UTF-16 编码的。由运行时投入一些急切的内存分配,您很可能会在预期之前达到 2 GB 标记。根据您对 blob 所做的操作,可能存在优化空间;更多流媒体是一种明显的方法。
  • @JeroenMostert 当我们得到基本上是 xml 字符串的 blob 时。我们希望将其转换为对象以进行额外处理。
  • 这并不排除使用流媒体。您可以使用blobClient.DownloadStreaming()BlobDownloadStreamingResult 直接访问Stream。然后,您可以使用该流创建一个XmlReader,它可以在您构建对象表示时使用selectively read from the blob,或者(如果您真的想拥有这一切)使用XElement.Load。这些方法中的任何一种都将绕过生成巨大的中间字节数组或字符串。

标签: c# .net azure azure-blob-storage


【解决方案1】:

• 我建议您使用'StorageTransferOptions'.Net Azure SDK 中用于调整性​​能的类。 This class has further several values which if defined in the .Net blob storage code can ensure that the transfer size of the blob and the concurrent connections to be initiated for parallel subtransfers to happen。他们是“最大并发”‘最大传输大小’.

使用时的“MaximumConcurrency”类会限制 .NET 的异步并行操作的连接池限制.同样,当使用“MaximumTransferSize”类,它限制下载操作期间支持的子传输的最大数据大小.因此,if you use these classes in your .NET Azure SDK code, the memory size consumption in your local system during downloading blobs can be checked 并得到照顾。

请在您的 .NET Azure SDK 代码中找到以下代码示例,以了解上述类的用法:-

  // Path to the directory to upload
string downloadPath = Directory.GetCurrentDirectory() + "\\download\\";
Directory.CreateDirectory(downloadPath);
Console.WriteLine($"Created directory {downloadPath}");

// Specify the StorageTransferOptions
var options = new StorageTransferOptions
{
    // Set the maximum number of workers that 
    // may be used in a parallel transfer.
    MaximumConcurrency = 8,

    // Set the maximum length of a transfer to 50MB.
    MaximumTransferSize = 50 * 1024 * 1024
};

List<BlobContainerClient> containers = new List<BlobContainerClient>();

foreach (BlobContainerItem container in blobServiceClient.GetBlobContainers())
{
    containers.Add(blobServiceClient.GetBlobContainerClient(container.Name));
}

有关上述类的更多信息和进一步说明,请参阅以下文档链接:-

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-scalable-app-download-files?tabs=dotnet#run-the-application

https://devblogs.microsoft.com/azure-sdk/tuning-your-uploads-and-downloads-with-the-azure-storage-client-library-for-net/

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 2021-03-29
    • 2021-07-25
    • 2016-02-29
    • 1970-01-01
    • 2020-08-22
    • 2019-03-23
    • 2018-05-07
    相关资源
    最近更新 更多