Ok, I'll admit I don't know much about how computers work, how they assign memory etc. Anyway I've been using C++/SDL to play around with a few things and I've made a simple programme that basically puts an image to the screen and prints out what the FPS is and caps it at a set amount. This seems pretty simple to me, something that shouldn't use much memory. But when i run it and look at task manager its using nearly 2,000,000 K (what is a K by the way? i dont know what it is but its using a hell of a lot more than any other programmes that are running!). Is this a good way of monitoring memory usage? and why would such a simple programme use so much? Also, it starts off at a normal level, but rises by about 20,000 K every update, which makes me think i may have some kind of memory leak, but then it stops when it gets to 2,000,000 K, and the programme still runs fine. I simply don't get it!
Another quick question, i tried this simple code:
1 2 3 4 5 6 7 8 9 10 11
int main() {
int numbers[250000];
for (int i = 0; i < 250000; i++) {
numbers[i] = i;
}
cout << "Memory used: " << sizeof(numbers)/1024 << "kb\n";
cin.get();
return 0;
}
to see if i could relate the K in task manager to the memory being used. It works fine for me with 250000 numbers but if i were to try 300000 the programme crashes. Its almost like its running out of memory but 300000 ints only needs about 1 mb memory. confused.
K probably means kilobyte (kB)
2,000,000 kB is almost 2 GB. That is a lot.
That the memory usage goes up sounds like a memory leak but I don't know why it stops.
In the example you are creating the array on the stack. The stack has limited amount of memory. If you use dynamic allocation on the heap it will probably work better.
I see, I didn't realise the stack was so small, <1mb seems small to me anyway, when you consider computers have gigabytes of RAM. You're right, works fine on the heap, and K does seem to be kB, when i ran it with millions of ints the memory being used in task manager roughly matched what it should have needed for that many ints. Don't know why they couldn't have just put an 'b next to the 'K'! Thanks.
Still confused as to why my other programme needs so much memory though.
*EDIT*
Solved my memory problem, i was creating an extra SDL_Surface everytime i looped through my code which was not being deleted. still not sure why it would reach a plateau of memory usage though. bit odd. but my programme now only uses about 6,000kB, much more sensible!
reserving memory for 300000 integer in runtime it leak the memory
that's way new comment come
it reserve the memory for it before do anything with the value