3

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

I've googled what is the best way to do this and found some information about CLR Runtime Hosting. It seems to be the thing I need, but I can not find any good examples.

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

Thanks in advance.

Andrey
  • 5,906
  • 4
  • 24
  • 30
  • This is an interesting topic. However, could you please elaborate a bit more *why on a per-class basis*? Maybe there's some other obvious solution if you unlock your thinking from class instances. – Ondrej Tucny Jan 30 '11 at 13:37
  • Actually it is not so important to limit on a per-class basis. A class of exact type is the only thing I'm going to instantiate, that's why my thoughts were 'locked' with class instances :). I can do it, for example, in the appdomain (create each instance in appdomain), but I can not find the way to limit a memmory for each appdomain :(. My aim is to limit somehow a memory that is available for 'external developer'. – Andrey Jan 30 '11 at 13:48

2 Answers2

4

This is not something you will be able to do through CLR Hosting. If you host the CLR, you can fulfill allocation requirements from the GC to Windows, e.g. so that instead of VirtualAlloc it uses some other allocator. However, the host is not invoked every time an object is allocated (this would be too expensive).

You could theoretically accomplish this by using the CLR Profiling API. It does allow you to receive a callback whenever an object is allocated.

I'm afraid, though, that you're trying to look at this from the wrong perspective. Instead of limiting the amount of memory used by instances of a class, which is very granular, could you instead try and isolate these external assemblies into separate processes, possibly even limiting them using the Win32 Job Object APIs?

Sasha Goldshtein
  • 3,499
  • 22
  • 35
  • Theoretically, there can be hundreds of such assemblies and they are in some kind of interaction. That's why I'm afraid of isolating each of them into a separate process. But of course, if there are no more options to limit memory except of per process, I have no choice :) Thanks for you response – Andrey Jan 30 '11 at 12:48
  • From my experience, the standard procedure in these cases would be to run multiple plugin assemblies in a separate process, perhaps each in a separate AppDomain. In general, you probably have "your" code which you trust and the "other" code which you don't, so there aren't too many layers of isolation. – Sasha Goldshtein Jan 30 '11 at 19:54
0

If you aim is limitate individual process memory consumtion, I think you shold use 'MaxWorkingSet' property of Process class. See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.maxworkingset.aspx for details

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
  • My aim is to limit the amount of memory for each specific class instance in 1 process – Andrey Jan 30 '11 at 13:10
  • Check this thread http://stackoverflow.com/questions/4752439/memory-usage-of-class-instance-in-c – Anton Semenov Jan 30 '11 at 13:25
  • I've found only the way to determine how much memory is already used.. But my question is how to prevent an external 'developer' to use more than, for example, 10 mb. It is not ok for me to allow allocating everything and then to check how much memory is allocated.. – Andrey Jan 30 '11 at 13:54
  • You may try Profiling API as variant http://msdn.microsoft.com/en-us/magazine/cc300553.aspx – Anton Semenov Jan 31 '11 at 07:52