Faqts : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 4 people (100%) answered Yes
Recently 4 of 4 people (100%) answered Yes

Entry

How can I just exit without freeing all the objects still in memory?
My program runs fine but is very slow to exit. Can I speed it up?

Sep 9th, 2002 09:20
Michael Chermside, Roy Smith, Gerhard Haring


The normal way for a program to exit is to call sys.exit(), raising a
SystemExit exception, or simply by completing the "main" script. These
are all equivalent, and what they do is to raise the SystemExit
exception, thus unwinding the stack, freeing objects in memory, and
finally terminating the program.
Normally, that's exactly what you want, but there can be exceptions. For
instance, suppose your program builds a very large data structure in
memory, performs some operations on it, outputs the results, and then
exits. If the data structure is complex enough, it could take quite a
bit of time to free all those objects.
The alternative, exiting without freeing the objects, is a little risky.
The operating system will reclaim all of the memory when your process
exits, but some of the objects may be written to do essential cleanup in
their __del__ methods... by not cleaning up you run the risk of leaving
lock files around, failing to close down files properly, and so forth.
But if you're sure that it's safe and you want to exit quickly, the
function to do so is "os._exit()".