Valgrind is a famous tool to debug your memory leak, but what sad is it only supports Linux, yeah, not a perfect support even on Mac. But with the new Windows 10 Anniversary update, it’s happening. And works way much better than Mac with OS X. Let’s rock!
1. What, Linux in a Windows?
It is easy since in the new update, windows has Ubuntu as a subsystem. And it is neither a virtual machine nor some kind of a container. It is a subsystem, roughly speaking, all the functions will finally invoked by Windows sys-call. So this is a 1st class, blazing fast solution. But the prerequisite is you need a new Windows 10 version 1607 (Build 14393.187) or above.
2. How to install and use it?
When you search online, you will find that you need to join the Windows Insider program and open the developer mode. But no, you don’t need to.
win+sto open Cortana.
- search for
windows features, open
Turn Windows features On or Off.
Windows Subsystem for Linux (Beta), click to turn it on.
- After a restart you could now open your windows command line and input
bash, then enter.
- It will first download and install Ubuntu 14.04 from Windows store.
- Then you need to configure your default user and password to the system. This is just for this built-in Ubuntu, not Windows
- Afterwards, every time you need
bash, you just open CLI then
bashand enter, splendid!
3. Try it first time.
It is a Ubuntu, so you might simply use the command.
sudo apt-get install valgrind. Yes, it will work as a charm. But only for this part. If you want to use it, it will totally not work.
# valgrind -v ./test
You may abort at this stage, since it is a beta feature, and the translation in the Sys-call level may seems a big deal. And after googling, you decide to abort. But, the story never ends like this :)
4.How to deal with it?
A brief first:
You just need to compile Valgrind by yourself on your own machine.”
The idea is simple, the procedures is still easy but take a little bit longer than expected. But I will cover all the commands :)
- Update your Ubuntu package lists by
sudo apt-get update
Install SVN first via this command:
sudo apt-get install svn
- Install subversion next:
sudo apt-get install subversion(Thanks to Ran Lottem)
- Find a folder you want to put the valgrind, anywhere is OK, we just need to compile.
- Download source code of Valgrind via SVN
svn co svn://svn.valgrind.org/valgrind/trunk valgrind. It will download the codes and put them into a new folder called
valgrindright under then folder you create or locate in step 3.
- Install the library used when compiling by
sudo apt-get install automake
- Ran Lottem confirmed that you need to
sudo apt-get install build-essential
- Go to the folder of Valgrind via
- Running the official bash script first by using
- Configure via
- From now on, things will get much more normal. first, install
sudo apt-get install make.
make, this command will build the files from many modules. Add
sudoif failed (Thanks Waleed Mahmoud).
make install, it will copy compiled files into appropriate locations. Add
sudoif failed (Thanks Waleed Mahmoud).
- It’s done already, but feel free to use
make clean, it will delete all the temporary files generated while compiling.
make distcleanwill make things much better.
valgrindas you wish.
5.Happily ever after
I have built a linked list to test the leak, it works exactly the same way as linux, even better than Mac, OS X, yes, I mean it. The valgrind on OS X will tell you have some leak problems while there is no leak.
camus@ALBERTSURFACEB:/mnt/d/github/C_Playground$ valgrind ./main
And if you have a leak, it will tell you.
==2539== HEAP SUMMARY:
valgrind --leak-check=full ./main will give the information of your stack. WOW! Fantastic! After I suffered so much from day 1 for the stupid Surface Book, this is the only time I feel Microsoft is a company you could rely on, haha xD
6. One more thing
Mr Artem reached out to me recently about his awesome software for debugging memory issues. He built Deleaker, which is a modern substitution of
Valgrind. Deleaker supports C as well as C++/Delphi/.Net etc. In general, it doesn’t matter what language is used to write code as it works at lower level, hooking allocation/deallocation functions and storing call stacks.
And having a nice GUI to work with just seems much more efficient to me, which is why I love jetBrains’ products.
- Check this blog for a comparison between Deleaker and Valgrind, why it is better.
- Check the Deleaker official site for more information.
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.