C# code to execute code in another application domain?
Asked Answered
F

3

5

I have a program that executes for 24 hours, then restarts.

How can I shift main() from into a separate application domain, which is torn down and refreshed every 24 hours, in order to completely eliminate any potential memory leaks?

Farrah answered 27/9, 2011 at 13:36 Comment(1)
Possible Duplicate with: Get access to AppDomain in another app? #6529749Cosine
V
19

You had me right up until you said:

in order to completely eliminate any potential memory leaks?

If you want to run code in another app domain then there are plenty of resources on how to do this, for example Executing Code in Another Application Domain (C# and Visual Basic). The basic principle is to create a class that inherits from MarshalByRefObject. You then create your new app domain and instruct it to create an instance of that object - this object is then your "entry point" into your app domain:

AppDomain newAppDomain = AppDomain.CreateDomain("NewApplicationDomain");
ProxyObject proxy = (ProxyObject)newAppDomain.CreateInstanceAndUnwrap("MyAssembly", "MyNamespace.MyProxy");

However in C# there isn't really any such thing as a "memory leak", at best you just have objects which are inadvertantly kept in scope. If this is the case then an app domain is just overkill - all you really need to do is remove references to managed objects that are no longer needed and the Garbage Collector will tidy them up for you.

If you have a true memory leak in unmanaged code an app domain won't help you either. Unmanaged types aren't bounded by app domains and so any unmanaged memory allocated "inside" the app domain won't be freed when the app domain is destroyed. In this case you would be better off using separate processes instead.

Vincevincelette answered 27/9, 2011 at 13:43 Comment(3)
This is all true and correct. However, I think the OP is expecting an AppDomain to get cleaned up when it's "closed" (including unmanaged resources) -- like windows does for a Process when it's exited. Unfortunately, however, there is no way (that I'm aware of) to unload a loaded AppDomain. One of the primary drawbacks of programmatically loading your own app domains into memory is that it will introduce memory leaks. Each AppDomain you load will be in memory until the process exits.Quintessa
@Quintessa AppDomain.Unload(newAppDomain). This isn't as good as isolating your code in a separate process, but will release memory allocated by the .Net GC, finalize objects etc... and most importantly, unload assemblies that have been loaded into that app domain.Vincevincelette
Ahh, my bad, I confused AppDomains with Assemblies (quite ridiculous of me, I know!). Thanks for correcting me there. -- (For others: See Jason Zander's blog: "Why isn't there an Assembly.Unload method?")Quintessa
N
9

I've created a class that allows you to execute code in a separate application domain which would allow you to dispose the application domain and recreate it: Executing Code in a Separate Application Domain Using C#

Nightshirt answered 2/1, 2012 at 22:15 Comment(1)
website is downApyretic
A
-3

You can't. A process is isolated and independent and you can't transfer a thread from one process into another process.

What you could do, however, if you absolutely can't fix the memory leaks internally, is create a watchdog program which launches the app whenever it stops running, and set up your app to only be run in single execution mode.

Ailing answered 27/9, 2011 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.