Codementor Events

Tips-Memory Management In Software Applications

Published Oct 21, 2018
Tips-Memory Management In Software Applications

In our day to day life while working on software , we all must have seen below kind of problems
• Application crashed
• Application Hanged
• Intermittent Issues
• Services Stopped
• Timeout issues
• Bad Performance Application
• While running one program another program crashed.
Any guesses why this happens??
The most common reason behind all these problems is ‘MEMORY LEAK’
“A memory leak is the gradual loss of available computer memory when a program(an application or part of the operating system) repeatedly fails to return memory that it has obtained for temporary use. As a result, the available memory for that application or that part of the operating system becomes exhausted and the program can no longer function. For a program that is frequently opened or called or that runs continuously, even a very small memory leak can eventually cause the program or the system to terminate.”
In layman’s language there are two ways to solve memory leakage problems..

  1. Make Application Small
  2. Get(Allocate) More Memory
    The best way out of this is (2) is allocate memory but whenever needed. In computer’s term this is called Memory Management.
    “Memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when no longer needed”
    One of best way of doing this is to do Memory Management at application side.

Here are some of best practices being followed in Industry for handling memory related problems at application side.
Best Practices: C#
• Create only the objects as and when needed and dispose it after use.
• Decide the scope for each variable and object, if they are required inside methods declare them inside those methods, do not make them private
• Use the IDisposable interfaces on your custom objects and release all the resources(if any), un-register from all the events etc
• Use ‘using’ if you are working with MemoryStream
• Use least static variables or instances, if required then too think twice whether those objects are required in entire lifetime of program
• Do not use GC.Collect() manually,(it is bad practice)
Best Practices : JavaScript
“Based on Garbage Collection as in C#”
• Always keep browser extension update
• Use Array Filter
• Use static buffers wherever possible. The compiler automatically frees such memory.
• Previously allocated memory should be manually freed, after it is no longer required.
• Use ~~ instead of math functions
• For emptying and array use length
• Merge array using push instead of concat
• Use splice to delete array element
• Get default value using ||
Best Practices: Python
“Not based on Garbage Collection but it handles memory management”
• Use Slots for your objects
• Interning: Beware of Interned Strings!
• Use Format Instead of ‘+’ for Generating Strings
• Use Modules
• Use built-in functions wherever possible:
• Use Multiprocessing not multithreading
• Design and Data Structure
• Pick right version
Best Practices: C++
“You need to write code about memory management “
• Reuse memory ; Try to avoid allocate/deallocate
• Use macros instead of small functions
• Use int,float instead of char
• Try to avoid global variables
• Use smart pointers
• Use static buffers whenever possible because compiler frees up such kind of memory automatically

Discover and read more posts from Bhawna Tuteja
get started
post commentsBe the first to share your opinion
Kashif Ali
5 years ago

Plz create 1 game any

Show more replies