Question:
Programming tips and run time memory?
anonymous
1970-01-01 00:00:00 UTC
Programming tips and run time memory?
Three answers:
?
2010-07-20 19:57:47 UTC
1. Measurement.



Either fire up 'Task Manager' (assuming you're on a Windows machine), or use more elaborate tools like Valgrind or GDB to just measure the virtual memory being used. There are a multiple of tools to do this, but the most basic way is to use task manager or an equivalent.



2. Yes, always dereference and free objects and memory bulks that are no longer required. Take time to chose the right algorithm and data structure for the task.



3. Yes, but this is implementation specific. Some languages run natively (and is compiled, like C/C++), others run in a virtual machine or an interpreter thus using more memory. Compilers also have the habit of optimizing code for you, a feature many basic interpreters lack.
?
2010-07-20 19:58:45 UTC
1.) Well the easiest way would be to open up task manager. To do this press ctrl+alt+delete. Click start task manager. Now click performance tab (near the top). Now run the program and see how big of a jump it made. This will tell you how much ram it uses. Unless you are using a HUGE program you don't need to worry about ram.

2. Generally programers don't worry about memory usage, but worry about performance of the program instead. You want to use functions that take less clock cycles to use to get better performance. Also things like "bubble sort" are very slow, so the performance is very poor.

3. Each program you are running will take up so much memory, but they may not take up very much each. You can run two of the same program and it will still take up 2X the memory of one program running. The run time depends on what functions you use, see above the bubble sort function. the language does not matter, because it isn't the actual language making things faster/slower it's the efficiency of each command you ask the computer to do. Hope this helps!
?
2010-07-20 13:23:47 UTC
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.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...