All Your Base Are Belong To Us

Mostly .NET internals and other kinds of gory details

Shutdown Performance: Just Exit!

The best shutdown sequence for an application is the shutdown sequence that doesn't require any work.

If all resources held by your application are process-wide resources that do not affect the rest of the system, then your shutdown sequence doesn't require any work.  All you need to do is let the system close you down.

Many resources fit in this category - memory, for one, is something that you shouldn't be obsessively freeing when asked to shutdown.  Windows will reclaim the memory, don't worry about it.  Handles to kernel objects are another example, although some of them don't like being abandoned (e.g. mutex handles would rather be closed or released than abandoned in mid-acquire).  Database connections, on the other hand, represent a resource that might take some time until it's reclaimed by a timeout.

The primary reason for minimizing the shutdown sequence as much as possible is fairly simple.  If your application hasn't been used for quite a while, then your shutdown sequence might result in lots and lots of paging.  There are myriads of applications that take a long time to shut down, and the disk LED starts flashing repeatedly for several minutes.  During the process, you're paging in memory that is completely useless (strictly speaking, it's needed for your shutdown sequence, but...) and you're causing other memory that is useful to be paged out, effectively incurring yet another series of paging activity when your application has finally been closed.

But why would the user close your application after it hasn't been used?  I hope you're not asking this question, but just in case you are, here are some common reasons:

  • The system has just come out of standby or hibernation, and the user wants to shut down or restart the system.
  • The user has just finished watching three consecutive episodes of his favorite TV show, and while going to bed decided to shut down the computer.
  • The user has just finished reading his 300-message email backlog, with your application in the background, and now wants to shut down the system and go to bed.
  • The user has been prompted with some Windows Updates installation, and during the process was incapable of using the system.  Now that updates have completed, the user wants to restart the system.

Wrapping up this short piece of advice:  Don't start freeing memory and obsessively validating resources when the user has requested a shutdown.  If your application hasn't been used for a while, you're now paging the entire world back in from the disk.  Do the bare minimum and get out!

Comments

Dew Drop - July 12, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 12, 2008 | Alvin Ashcraft's Morning Dew

# July 12, 2008 4:30 PM

Arjan`s World » LINKBLOG for July 12, 2008 said:

Pingback from  Arjan`s World    » LINKBLOG for July 12, 2008

# July 12, 2008 11:32 PM

Doogal said:

whilst I agree, there is a problem with this approach. Say you've got a memory leak and you use a memory profiler to figure out what is causing the leak, if you don't free up resources on shutdown, you'll get a lot of false positives in your results. Not sure if you can recommend a way round this?

# July 14, 2008 1:14 AM

Sasha Goldshtein said:

You don't have to wait until application shutdown to find a memory leak.  If you suspect there's a memory leak that manifests after an hour, take a look at what objects you have after that hour has elapsed.

I concede that that some frameworks rely on freeing objects on shutdown as a leak-detection mechanism - MFC does that, for example.

# July 14, 2008 3:54 PM

Jorge said:

I'd usually agree with you and will let Windows clean up every resource it can ... but there are some exceptions to it.

Even though it's not a big deal, I've find that sometimes Windows takes some times to take clean up your app, I'm talking big apps, I had once an application which will require as much memory as you can give it (expert system) so when the time came to close it Windows had to free about 1 or 2 GB of virtual memory (maybe more, depending on the system).

Of course main screen just disapeared but some users keep asking why the app took SO long to disappear from the task manager and start complaining about the application hanging while exiting (which wasn't true) so we finally had to display a little screen saying the application was closing... free all resources and then exit the application.

# July 15, 2008 12:03 PM

Sasha Goldshtein said:

Jorge, was freeing those resources any faster than leaving the job to Windows itself?

I'm not condescending here as I don't know what kind of users your application has, but most "users" I've encountered would much rather have the application immediately disappear (even if it still lingers on in Task Manager, which they normally don't look at).

# July 16, 2008 3:36 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: