How to limit the memory that can be allocated to a specific class?

I load several external assemblies into my application at runtime. And I need to limit the amount of memory that can be used by a certain class, which is defined in each of the external assemblies, for example, 10 mb per instance, otherwise we get OutOfMemory.

I was looking for the best way to do this and found some information about CLR Runtime Hosting . It seems to me that this is what I need, but I can not find good examples.

Can anyone share some code examples or maybe some links about memory management using CLR Runtime Hosting? Or maybe there are some better solutions to limit the amount of memory in a class?

Thanks in advance.

+3
source share
2 answers

This is not something you can do through CLR hosting. If you host the CLR, you can fulfill the distribution requirements from the GC to Windows, for example. so instead of VirtualAlloc it uses a different allocator. However, the host is not called every time the object is allocated (it would be too expensive).

Theoretically, you can do this using the CLR profiling API. This allows you to receive a callback whenever an object is highlighted.

, . , , , , , , API- Win32?

+4

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


All Articles