The best way to avoid heap memory problems is by using the variable declaration with appropriate scope. The usage of having global variable should be reduced as much as possible. and keeping unused memery block without clearing it. In game programming the main issue is having an image in memory. Only those images which definitly has to be in the memory and the other have to be cleared regularly.