Why does Automapper use so much memory?

I use the latest version of Automapper (v3.0.0.0-ci1036), and when it converts an object with binary data, it uses crazy amounts of memory. (200 MB for a 10 MB file). Here is an example of such a convertible "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; } } 

Is there something wrong with my code, or do I need to stop using automapper for objects containing binary data?

+4
source share
1 answer

I am not sure, but the reason may be the following:

Your C # application runs in the .NET runtime, which, when possible, clears heap memory using the garbage collector.

This method has a side effect for fragmenting your heap memory. So, for example, you can have 100 MB allocated, with 40% available for new variables that are fragmented in smaller pieces of max. 5 MB

In this situation, when you allocate a new 10 MB array, the .NET virtual machine cannot allocate it even if it has 40 MB for free.

To solve the problem, move your available heap memory to 110 MB (at best) and allocate a new 10 MB for the new byte array.

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

0
source

Source: https://habr.com/ru/post/1485429/


All Articles