I know how to use malloc()
and free()
to allocate memory, but is there also a standard C function to check how much memory is left, so I can call that periodically to make sure my code has no memory leaks?
The only thing I can think of is calling malloc(1)
in a endless loop until it returns an error, but shouldn't there be a more efficient way?
No, there's no standard C function to do that. There are some platform-specific functions you can use to perform certain types of queries (like working set size), but those probably won't be helpful, because sometimes memory which has been properly
free()
d is still considered to be allocated by the OS because themalloc
implementation might keep the freed memory around in a pool.If you want to check for memory leaks, I highly recommend using a tool like Valgrind, which runs your program in a virtual machine of sorts and can track memory leaks, among other features.
If you're running on Windows, you can use
_CrtDbgReport
and/or_CrtSetDbgFlag
to check for memory leaks.If you can afford #ifdef'ing a debug version (possibly in an emulator!), you could just build a debug version of malloc/free that keeps track of the number of bytes currently in use, and "print" it periodically (again - only in the debug version, possibly under an emulator) on whatever output device you have for debugging (a led?), and see if it keeps increasing.
The standard trick is to allocate sizeof(size_t) more than requested, thus storing the size together with the newly allocated memory - but if you're writing a firmware I guess you know it already :)
So... do you have an emulator?
EDIT: I'm so used to computers running at GHz that it didn't occur to me at first, but of course another thing you can do is to just count the number of allocations, not their size -- I can't imagine how this could take too much memory to run.
Linux glibc
sysconf(_SC_AVPHYS_PAGES)
andget_avphys_pages()
These two glibc extensions should give you the available number of pages. We can then just multiply that by the pages size
sysconf(_SC_PAGESIZE)
to find the total available memory.main.c
GitHub upstream.
Compile and run:
sample output on my 32GiB RAM system:
0x7CCFFC000 is a bit smaller than 32GiB, and is the total RAM. 0x6383FD000 is the available one.
man get_avphys_pages
says it gets its data from/proc/meminfo
.Tested in Ubuntu 19.04.
I searched and found this question to help me make an application to animate numerous iterations of fractal functions across multiple arrays of complex values.
Thank you, Alexey Frunze, for your ideone.c code. It has certainly been helpful.
Building upon what I learned, in hopes of more helpfulness, I have written the following:
Usage:
Input:
Output:
I intend to re-purpose these functions into my own application.
If in your system
malloc()
always allocates physical memory, you can callmalloc()
repeatedly with sizes differing not by 1, but by successive powers of two. That'll be more efficient. Below is an example of how to do it.If, on the other hand,
malloc()
only allocates virtual address space without mapping physical memory into it, this won't give you what you want.Sample code:
Output (ideone):