Monday, 8 August 2011

why should we use destructors? However memory is released when the program terminates?

Simply put, because you don't always want to wait until the end of the program to free resources that are no longer needed by the application.
For example, if you were to hold on to the memory allocated every single time the program calls a class object, or every time the program performs a calculation, or every time the program performs any operation, then eventually your memory usage for a larger, more complex program, would be off the scale.
What might start off as a few bytes here or a couple of kilobytes there would eventually work into megabytes of unreleased memory resources and slow down the operation of your application. Assuming that the application then continues to run for an indefinite amount of time (For example, I don't always close every browser window in my FireFox application and leave 1 or more windows open for days at a time) then those megabytes of unreleased memory become gigabytes of unreleased memory.
As you can see, if you assume that leaving the memory cleanup to the closing of the application will be sufficient you can cause some rather serious memory usage issues with your application

No comments:

Post a Comment