3

If I do (for example)

 for (int i=0;i<22;i++)
 {
     var app = new Excel.Application();
 }

then 22 excel processes are created.

However, if I do

 for (int i=0;i<25;i++)
 {
     var app = new Excel.Application();
 }

It creates 22 excel processes, but then most of them dissapear and only a few are left.

Is there a limit of 22 or something? Can this be increased?

Thanks!

EDIT: with following code it doesn't occur:

 var apps = new List<Application>();
 for (int i=0;i<25;i++)
 {
      apps.Add(new Application());
 }
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • 2
    catch 22 from Microsoft? how much available memory do you have? BTW, what are you trying to achieve except heating your processor? – Aprillion Jun 22 '12 at 15:14
  • 4
    Is this as easy as: Your var app is going out of scope and the garbage collector is cleaning things up? – Joel Rondeau Jun 22 '12 at 15:21
  • @deathApril: 12 gig of memory. Well it's a dedicated server that uses excel to automate calculations. – L-Four Jun 22 '12 at 15:23
  • 1
    @ Joel Rondeau: yes, that seems to be the problem! – L-Four Jun 22 '12 at 15:24
  • 1
    @Lud please post your solution as an answer (that `var app` was garbage collected, so you solved the problem using a `List`) – Aprillion Jun 22 '12 at 15:32

2 Answers2

2

Garbage collection kicked in... with following code, it works:

 var apps = new List<Application>();
 for (int i=0;i<25;i++)
 {
      apps.Add(new Application());
 }
L-Four
  • 13,345
  • 9
  • 65
  • 109
0

I remember an article by Raymond Chen. Something about if you load too many of the same process for your specific machine, the OS cuts you off. It's different for every machine (as it's based on the performance of your machine).

However, I can't see 25 as being a limit. That seems slightly too small, even for older machines. Are any of your files referencing data from the same files causing some of them to close prematurely because another instance of Excel is locking the file?

I'd need more information to find out what is actually going on.

Nick Funk
  • 140
  • 2
  • 9
  • Hi, well, actually it's just a console application with the for loop, no additional code is involved in this test. No other excel files are open. I ran it on my (fast and recent) computer, and on another computer, both with same result: it seems that after 22 excel instances some cleanup mechanism starts. – L-Four Jun 22 '12 at 15:19
  • 1
    See the (Lud's) above answer. That's why I asked for more information, I assumed you were initializing with var and saving that declaration to an external list. If you are only declaring `var app` within the loop, then your instances will get cleaned up once you leave the scope of the loop. Lud is right, garbage collection was kicking in. – Nick Funk Jun 22 '12 at 15:57
  • 2
    I may just have haha. It's been a long morning. Cheers. – Nick Funk Jun 22 '12 at 16:10