5

我正在使用最新版本的 Automapper (v3.0.0.0-ci1036),当它使用二进制数据转换对象时,它使用了大量的内存。(10MB 文件为 200MB)。这是一个正在转换的“文件”示例:

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; }
}

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

4

1 回答 1

0

I am not sure, but your reason might be the following:

Your C# application works on the .NET runtime which clear the heap memory when possible using the Garbage Collector.

This technique has the side effect to fragment your heap memory. So, for example, you might have 100MB allocated, with the 40% available for new variables that is fragmented in smaller chunks of max 5MB.

In this situation, when you allocate a new array of 10 MB, the .NET virtual machine has not room to allocate it, even if it has 40MB free.

To solve the problem it move up your available heap memory to 110MB (in the best case) and allocate the new 10 MB for your new byte array.

Also see: http://msdn.microsoft.com/en-us/magazine/dd882521.aspx#id0400035

于 2014-11-11T12:08:55.653 回答