Krista's Coding Corner


Memory leaks

Other kind of a bug is a memory leak. You normally don’t create these with Python but with C and C++ (etc.) these are a really big problem. Memory leak happens when program doesn’t free the memory it is using when it doesn’t need it anymore. Every program uses memory, if it is doing something. You know: computesr also have to remember what they were doing ;)

Memory leaks slows programs down as time goes on. These bugs work a bit like rolling snowballs: they get bigger and bigger as time goes by and they roll forward. Think that you would be drinking milk: every time you take just a little sip from the milk carton, you will abandon the carton, even it is almost full, and take another. Quite soon you will have a massive amount of opened cartons with milk inside them. Not so effective use of the milk cartons. Or just think that you would like to borrow a book from a library. What would you think if one person had borrowed almost all of the thousands of books and never returning any? Memory leak means grabbing more and more memory to yourself even you don’t need it.

Compilers won’t tell you that you have memory leaks, so you have to use some program for this. One is Valgrind, which I strongly recommend you to use if you do anything, ANYTHING, with C or C++. Valgrind is your friend. Make using Valgrind (or something else) a habit from the start because it is incredible pompous not to use it. Also not using it gives really unprofessional image, along creating complex programs that become unfunctional.

Any guesses what is one of the main factors why Windows becomes slower and sticky after long use?

What to do, if you notice a memory leak (on your own code!)? Well, depends on the used language but if you were able to somewhere along your code reserve memory you should be able to undo it after use. There is a command to do it as there is a command to do the reserving. Just read some tutorials :)

blog comments powered by Disqus