none
Application domains - out of memory exception

    Question

  • Hi.

    I have been developing an application that measures performance on concurrent runtime compilation in single and multiple domains.

    I have a problem with creating necessary number of application domains. As far as I know there is no limit in the number of application domains created. But practically this number is limited by virtual memory allocated.

    When I execute the following code:

    static void Main(string[] args)
    {
        for (int i = 0; i < 1000; i++)
        {
            Thread thread = new Thread(new ThreadStart(Process));
            thread.Start();               
        }
    }

    static void Process()
    {
        AppDomain domain = AppDomain.CreateDomain(String.Format("Domain {0}", Guid.NewGuid()));
        AppDomain.Unload(domain);
    }

    an exception (HRESULT=0x800700e - out of memory) is thrown and the process is killed.

    How can this situation be handled?
    Thursday, April 05, 2007 9:34 AM

Answers

  • Your test code doesn't actually create a bunch of AppDomains, it unloads them right away.  Although that doesn't happen immediately, the AppDomain cleanup normally takes time.  Furthermore, the thread start and exit quickly.  I doubt that this is a realistic simulation for what your actual program does.

    AppDomains and threads are expensive resources.  An AppDomain takes about 120 KB, a thread needs about 1100 KB of virtual memory space.  With these kind of numbers, it is not hard to generate an OutOfMemoryException...
    Thursday, April 05, 2007 7:24 PM

All replies

  • Use a 64-bit machine and be sure to plug enough memory into your machine.

     

    On 32 Bit Machines:

    You can set the stack size while creating a thread set a smaller stack size to create more threads (1MB is the default stack size).

    This can cause StackOverFlowExceptions on the other hand....

     

    A try catch clause around your main method could also help a bit but since the OutOfMemoryException can happen in any thread you never can be sure....

    To work around the exception you can use the MemoryFailPoint class to get an InsufficientMemorExcepiton which helps you to deal with such problems on the safe side.

     

    Yours,

       Alois Kraus

     

    Thursday, April 05, 2007 12:45 PM
  • I tried your code.  It works fine when you run it outside of the IDE or start it in the IDE without debugging.  However, in debug mode, you're giving the debugger a serious headache keeping up with so many AppDomains and threads.  Memory usage climbs quickly until the debugger gives the ghost with a "fatal error".  Your mileage may vary.
    Thursday, April 05, 2007 3:11 PM
  • I have tried to run it outside of IDE too, but I had the same problem. May the reason be in hardware?
    Thursday, April 05, 2007 7:00 PM
  • Your test code doesn't actually create a bunch of AppDomains, it unloads them right away.  Although that doesn't happen immediately, the AppDomain cleanup normally takes time.  Furthermore, the thread start and exit quickly.  I doubt that this is a realistic simulation for what your actual program does.

    AppDomains and threads are expensive resources.  An AppDomain takes about 120 KB, a thread needs about 1100 KB of virtual memory space.  With these kind of numbers, it is not hard to generate an OutOfMemoryException...
    Thursday, April 05, 2007 7:24 PM
  • Could you please share a link where I could read more about overheads of cleanup of application domains? Or overall overheads of their usage?
    Friday, April 06, 2007 9:30 AM
  • That's not documented anywhere I know of.  I just measured it.
    Friday, April 06, 2007 10:35 AM