10

I have a website where some user can upload his dll. I need to create an instance from this dll and call a specific method. Moreover, I need to do it with several dll's in 1 process, so it is not an option for me to use System.Diagnostics.Process.

Is there a way to determine how much memory does this method call use or limit it in my application runtime? Thanks in advance for any help

1
  • BTW, if one of us answered your question, can you please accept the answer by clicking the tick next to the question. Thanks Commented Jan 25, 2011 at 20:38

4 Answers 4

9

Memory usage in .Net is very difficult to gauge - It will depend on a lot of factors like whether garbage collection run, how much of the framework has been loaded, etc.

You can look at memory usage on a per-process level using performance counters

Assuming for a moment you've already considered the security implications of running a users code in a semi-trusted environment, you need to use an AppDomain to load the DLL - That way it can be unloaded without restarting the whole website / worker pool

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot for you answer. I will try to explain more precisely what do I need. :) My project is a game for developers, where they need to upload their code and "fight" against each other. So after there are some uploaded dlls, we can create a round to determine who wins :). I thought about creating this round in a separate process, and in this process I was going to create instances and call methods from uploaded dlls. And I need to limit memory usage for this instances, so if developer runs out of this limit - he loses the round.
Ah, now I understand. Seperate process is definitely required - You might even consider a process-per-dll depending on how many you're expecting to host at once - That way you can still monitor memory usage using performance counters.
Another option would be to expose a "heap" ala Byte() or something a little more clever (Dictionary?) to your developers which they have to use for all variables - you can then control the Byte() explicitly and use reflection to make sure they're not allocating memory inside their assemblies.
Oh, how could not I find this solution earlier by myself :) Thanks a lot. But I will try to work with performance counters first. It is a new interesting thing for me :)
Glad I could help :) Welcome to SO - Hopefully we'll see you more in the future
|
5

If you use .NET 4.0 have a look at MemoryFailPoint.

Another suggestion: .NET 4.0 now offers facilities to supervise the memory consumption of an AppDomain (AppDomain.MonitoringIsEnabled Property). You can for instance use the main AppDomain to poll and tear down an AppDomain, if it uses too much memory.

Comments

4

Could you use GC.GetTotalMemory as an alternative to track your memory usage?

http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx

Comments

0

Seriously you cannot go past Red Gates Ants Memory Profiler - free 14 day trial...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.