Previously we looked at how selecting programming constructs wisely can help you avoid slow code. The importance of memory management to keep your code up to speed takes center stage in this article.
Poor memory usage is also an area that can slow down application execution. Even in managed environments, such as .NET, attention to memory is important, even though the system allocates and reclaims all memory. Improper memory management can lead to large numbers of long-lived objects, huge objects that serve no programmatic purpose, and to object leaks. All of these contribute to memory bloat and slow execution. If not resolved, they can ultimately lead to application crashes and out of memory errors.
Within managed environments, many programmers still prefer managing their own memory, and the .NET Framework provides a limited ability to do so. However, the garbage collector in the .NET Framework will almost always do a superior job of managing memory than the programmer.
There are techniques available to the programmer to make sure that the memory footprint doesn’t grow unnecessarily. One important goal for programmers at this point is to ensure that as many objects have as short a life as possible. This is done by making as many variables local as possible, and enabling them to go out of scope when their method is complete. Once out of scope, their memory can be quickly reclaimed by the .NET garbage collector.
What is your experience with managing application memory in your development? Share them with us below.
Blog article submitted by Peter Varhol, Technology Strategy Research
This blog series continues with part three tips for managing database calls to improve programming performance under the .NET framework.