You are correct, RAM is the more common term. But "runtime memory" is probably a more "correct" term, as both RAM and cache usage could be included in "runtime memory." But a computer's cache is extremely small (just a couple megabytes), so it's generally not considered a large part of runtime memory usage. I'll just say, "memory usage" if that's okay with you. :-)
1. How can you tell how much memory a program written in C++ takes while it is running
Depends on your operating system. But, assuming you're running Windows, you can see the memory usage in the Task Manager. Just right-click your taskbar (start menu bar) and choose "Task Manager." Then click the "processes" tab and you will see a list of processes (including your C++ program) and each one will show the process's memory usage under "Mem Usage."
2. Are there any tips or like rules-of-thumb which i could use to minimize the run-time memory my program uses?
Beware of memory leaks. C++ does not have built-in garbage collection. Garbage collection is a system used by many programming languages (like Java), which goes around and reclaims memory that's no longer in use (freeing it up for other programs). Without garbage collection, you need to manage your own memory. Therefore, whenever you allocate memory (like, through the "new" keyword or through "malloc"), you need to make sure to designate that memory as free later on (by using the "delete" keyword or "free", for instance). Otherwise, that memory could be "leaked."
Take a look at this code:
#include
#include
using namespace std;
int main() {
int* leakingPointer;
int count = 0;
const int size = 10 * sizeof(int);
for(;;) {
leakingPointer = (int*)malloc(size);
printf("Allocated %d more bytes! Total allocation: %d bytes!\n", size, (int)size * ++count);
}
return 0;
}
It will continually allocate memory over and over and over again without freeing it up. The result (if you were to run the program and watch in the task manager) is that it uses more and more memory until you kill it (or until Windows kills it). I got it to use 700MB of memory on my MacBook Pro before I killed it myself.
So, in short, to keep your usage low, just make sure you manage memory correctly, yourself.
3. let's say there are a bunch of programs which all do exactly the same thing - would they take up different run time memory depending on what language they're written in? if so - which language takes the least run time and why?
Yes. Some languages have a certain amount of overhead. Some languages load large libraries when they run. Some languages include systems to support the program that take up memory. For instance, a Java version of "Hello, World" takes more memory than a C++ version because Java has something called the "JVM" that needs to be loaded in order to run a Java program. So, even though the program is extremely small, using Java necessitates the JVM, and therefore, a certain amount of overhead.
But Java also comes with garbage collection, so if you have a large (poorly written) program, Java may help REDUCE the memory usage. So just because a given program may work better or worse in another language, doesn't mean that a language is necessarily superior in terms of memory usage.
Generally, when you're looking for low overhead, you should look to C or C++. However, while those languages are extremely compact and efficient, they don't come with a lot of help in the way of auto-magic memory management. So if you want your program to be small, do it in C/C++. But make sure you do it right, or you will end up using far more memory than you would with a Java version.
Hopefully that wasn't TOO technical. I assumed a working knowledge of C++. Hopefully that wasn't too much.