Hi there,
I'm brand new to C programming.
I was wondering if somebody could demonstrate a program how you could get the available RAM on my computer and write it to a text file?
Many thanks
Stev
Hi there,
I'm brand new to C programming.
I was wondering if somebody could demonstrate a program how you could get the available RAM on my computer and write it to a text file?
Many thanks
Stev
That would depend on your OS and Compiler.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hi,
I'm using a gcc compiler on a ubuntu OS.
Thanks
Stev
Parse the contents of /proc/meminfo, and do what you need with it.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
What Elkvis said. The fields are listed in man 5 proc.
Because the Linux kernel uses free memory for caching filesystem accesses, the amount of RAM available ("free") is roughly MemFree + Buffers + Cache.
It does not mean a program can expect to be able to allocate that amount, as that is completely different policy, and can be controlled per user an so on; it also depends on whether or not overcommit (allow more than physical RAM available in allocations) is enabled. In other words, an application might be able to allocate more or less than that, depending on the OS configuration.
The amount of "free" memory is rarely useful information to an user. In particular, most applications are snappier when a lot of memory is used in Cache, because then the application and library files, as well as the data files used, are cached in RAM. That way they're immediately available, and you don't need to wait for the files' contents to be read from disk. Because the latencies on human actions are shorter, the applications feel snappier.
The only time you want to clean the cache (and there is a single-line command to do that) is when benchmarking I/O-sensitive applications.
I have seen time and time again Windows users wanting to "free some RAM", desiring a higher MemFree amount, because they incorrectly assume that it makes applications snappier in sane operating systems. It does not. What Windows needs, is peculiar and weird, and is not portable to other systems in general.
For users, MemFree is useful only as an indicator how much more RAM they have than they actually need. Cached is useful as an indicator to show how large your typical working set is. That's it.