memory-leaks - 为什么 Automapper 使用这么多内存?

标签 memory-leaks automapper .net-4.5

我使用的是最新版本的 Automapper (v3.0.0.0-ci1036),当它用二进制数据转换对象时,它使用了大量的内存。 (10MB 文件为 200MB)。下面是这样一个"file"开始转换的例子:

class Program
{
    static void Main(string[] args)
    {
        convertObject();
    }

    private static void convertObject()
    {
        var rnd = new Random();
        var fileContents = new Byte[1024 * 1024 * 10];
        rnd.NextBytes(fileContents);

        var attachment = new Attachment { Content = fileContents };

        Mapper.CreateMap<Attachment, AttachmentDTO>();
        Console.WriteLine("Press enter to convert");
        Console.ReadLine();
        var dto = Mapper.Map<Attachment, AttachmentDTO>(attachment);
        Console.WriteLine(dto.Content.Length + " bytes");
        Console.ReadLine();
    }
}

public class Attachment
{
    public byte[] Content { get; set; }
}

public class AttachmentDTO
{
    public byte[] Content { get; set; }
}

我的代码有问题,还是我必须停止对包含二进制数据的对象使用 automapper?

最佳答案

我不确定,但您的原因可能如下:

您的 C# 应用程序在 .NET 运行时上运行,它在可能的情况下使用垃圾收集器清除堆内存。

此技术具有将堆内存碎片化的副作用。因此,例如,您可能分配了 100MB,其中 40% 可用于新变量,这些新变量被分成最大 5MB 的较小块。

在这种情况下,当您分配 10 MB 的新数组时,.NET 虚拟机没有空间来分配它,即使它有 40 MB 可用空间。

为了解决这个问题,它将您的可用堆内存向上移动到 110MB(在最好的情况下)并为您的新字节数组分配新的 10MB。

另见:
http://msdn.microsoft.com/en-us/magazine/dd882521.aspx#id0400035

关于memory-leaks - 为什么 Automapper 使用这么多内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17024305/

相关文章:

java - 获取有关特定内存泄漏的更多详细信息

java - 调试 JVM 内存泄漏

c# - Linq to SQL 和自动映射器

asp.net-mvc - 在 Automapper 中修剪字符串

json - 如何使用 Automapper 将 poco 映射到 JSON

jquery - 从 jQuery 调用 .asmx Web 服务 : GET is not allowed?

c# - .NET 4.5 中的代码契约 + 异步 : "The method or operation is not implemented"

ios - Swift iOS 项目在创建空项目后立即泄漏

java - 修复监听器保留隐式引用时的 Activity 泄漏

c# - VS2017 具有新的 getter/setter 语法 : How to write multiple lines in the setter?/