Creating and Generating .NET Objects

Is there any way to tell .NET to allocate a new object on the generation 2 heap. I have a problem when I need to allocate about 200 MB of objects, do something with them and throw them away. What happens here is that all the data is copied twice (from gen0 to gen1, and then from gen1 to gen2).

+3
source share
3 answers

It is impossible to isolate directly in generation 2. All distributions occur in generation 0 and for large objects on the LOH.

However, data is usually not copied when objects are moved to a new generation. Instead, the starting points of the different heaps move. Objects may be moved due to heap compaction, but this is a completely different story.

If it is important to store objects in generation 2, you can reuse instances. That is, make some init method for the types and call it instead of creating new instances.

+2
source

The Framework works differently with large objects, which with ordinary objects. Therefore, in short, he puts large objects in a generation and does not move them when heap is redistributed.

0
source

, , GCHandle . P/Invoke, , . , - , , .

http://msdn.microsoft.com/en-us/library/khk3k17t%28v=VS.71%29.aspx

Example:

var q = new MyObject();
var handle = GCHandle.Alloc(q, GCHandleType.Pinned);

// use object

handle.Free();
0
source

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


All Articles