145

What is an AppDomain? What are the benefits of AppDomains or why Microsoft brought the concept of AppDomains, what was the problem without AppDomains?

Please elaborate.

Linky
  • 605
  • 8
  • 24
Praveen Sharma
  • 4,511
  • 7
  • 27
  • 17

3 Answers3

130

An AppDomain provides a layer of isolation within a process. Everything you usually think of as "per program" (static variables etc) is actually per-AppDomain. This is useful for:

  • plugins (you can unload an AppDomain, but not an assembly within an AppDomain)
  • security (you can run a set of code with specific trust levels)
  • isolation (you can run different versions of assemblies etc)

The pain is you need to use remoting etc.

See MSDN for lots more info. To be honest, it isn't something you need to mess with very often.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 13
    One little (but important) thing to mention: AppDomains do not cover threads. – mmmmmmmm Feb 22 '09 at 13:04
  • @RüdigerStevens what do you mean?? – AgentFire Sep 30 '15 at 11:53
  • 6
    @AgentFire: If some code running in some thread and some AppDomain calls code from another AppDomain, then the thread "crosses" the AppDomain border and runs code from that other AppDomain. So threads do not belong to specific AppDomains...although one can say that a thread "belongs" to the domain the code that created it was coming from. But a thread can run code from any AppDomain. – mmmmmmmm Oct 02 '15 at 16:04
  • @RüdigerStevens that's actually an advantage. – AgentFire Oct 02 '15 at 18:37
  • 9
    As @Marc pointed out, thinking about an AppDomain as an extra Layer of isolation is a good idea. I would like to put this into a context: [Process > CLR > AppDomain > Assembly with statics > Thread with stack]. That means a process hosts a Common Language Runtime (CLR). The CLR has one or more AppDomains. Each AppDomain loads one or more assemblies. Each assembly has it's own static varibales and one or more threads. Each thread has it's own stack. – Doomjunky Jul 05 '16 at 23:31
  • i guess, it would be an abstraction leak, if threads weren't context-agnostic like this. also, if you look at thread as some machine traveling through the control flow, serially executing commands along the line, you'll see it doesn't care where this chain of commands will lead him, be it the same domain it started in or another. all commands look similar and are treated similarly, wherever they are "physically" situated. the notion of command's origin is orthogonal to what thread i and does in its one-dimensional world. heh. – jungle_mole Aug 01 '17 at 06:46
59

An App-domain implements the concept of a contiguous virtual memory space that holds the code and the in-memory resources that can be directly accesses or referenced.

Separate AppDomains do not share memory space and, consequently, one AppDomain cannot directly reference contents in another. In particular, data must be passed between AppDomains through a copy-by-value process. In particular, reference objects, which rely on pointers and therefore memory addresses, must first be serialized from the source and then deserialization into the destination AppDomain.

Previously on Windows systems, memory boundaries were implemented by processes; however, constructing processes is resource intensive. They also serve a dual purpose as thread boundaries. App-domains, on the other hand, are concerned only with memory boundary or address space. Threads can 'flow' across AppDomains (that is, a procedure can invoke an entry point in another AppDomain and wait for it to return. The thread is said to 'continue' execution within the other AppDomain).

One significant benefit of this architecture is that communication patterns between App-domains remain substantially unchanged whether the AppDomains are in the same process, different processes, or on a different machines all together: namely the process of serialization and deserialization (marshaling) of parameter data.

Note 1: the meaning of a thread crossing an AppDomain is that of a blocking or synchronous method call into another AppDomain (versus a non-blocking or asynchronous call which would spawn another thread to continue execution in the target AppDomain and continue in it's current AppDomain without awaiting response).

Note 2: there is such a thing as Thread Local Storage. However, a better name would have been App-Domain Thread Local Storage since threads leave their data behind as they cross App-Domains but pick them back up when they return: http://msdn.microsoft.com/en-us/library/6sby1byh.aspx

Note3: A .Net Runtime is a Windows Process application with an associated heap. It may host one or more AppDomains in that heap. However, the AppDomains are design to be oblivious of each other and to communicate with each other via marshaling. It is conceivable that an optimization can be performed that bypasses marshaling between communicating AppDomains sharing the same .Net Runtime and therefore the same Windows Process heap.

George
  • 2,451
  • 27
  • 37
  • Well, I was a bit confused between your statement **Separate AppDomains do not share memory space** and @Brian Rasmussen **AppDomains are part of the same process and thus actually share the same managed heap** . Can you clarify a bit ? – cat916 Aug 18 '16 at 02:39
  • 1
    When AppDomains are part of the same same Windows Process (and therefore within the same .Net runtime instance), they will necessarily be in the same managed heap of said Windows Process; however, this circumstance will normally be invisible to the .Net application. Remember, the .Net application is not a Windows Process application and actually runs in a kind of virtual machine. – George Aug 18 '16 at 07:19
  • this has to be voted up more. Everything's explained really well. – supi Jun 16 '21 at 16:03
37

AppDomains can be viewed as lightweight processes. They share many of the same characteristics of a process, e.g. they have their own copies of statics, assemblies and so forth, but they are contained within a single process. From the operating system's point of view a process is just a process no matter how many AppDomains it may contain.

Unlike a process however, an AppDomain does not have any threads unless you explicitly create them. A thread can run code in any AppDomain.

AppDomains are part of the same process and thus actually share the same managed heap. This is usually not an issue since the AppDomain programming model prevents implicit access between AppDomains. However, some references are actually shared between AppDomains such as type objects and interned strings.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317