Reputation: 361
I'm very frustrated over something very simple (or so I guess), so help in this matter would be very much appreciated. (Sorry if this has already been answered, I haven't had the luck of finding it somewhere else, that's why I'm asking...) So, I've written the following very simple program, for test purposes.
class myclass{
int x[99999];
public:
myclass(){}
};
int main(){
myclass *x = new myclass;
delete x;
}
Having used a break point in the 1st line of main, it's easy for me (using visual studio 2010 and windows resource monitor) to realize, that after delete is called, the reserved memory for this program is not released after delete x. If I change x[99999] in myclass, to x[999999] (adding an extra 9), then the allocated memory is indeed freed. I'm worried about this peculiar behavior (which happens in various similar tests), but as it is expected, this program is not really what's worrying me... My first problem is that, I'm not sure if I can trust windows resource monitor or not. Is it really updating its output all the time? Or just when certain amounts of space are (de)allocated? If it's the second, would you please recommend to me a tool that monitors resources precisely? Secondly, I have used a program (I don't remember what it was called) that checks an executable for memory leaks, for a larger project of mine. No memory leaks were found. However, a certain event would cause very small amounts of memory leak (according to windows resource monitor). Every 4 calls to a certain event would cause about 1 KB of memory leak. I'm quite certain that, that part of my code is correct, but similarly to the example above it reserves small amounts of memory, which windows perhaps doesn't "consider" worthy enough to deallocate when asked to, perhaps? Or perhaps each function call on windows collects some system garbage? (I doubt that...) Or perhaps heap calls accumulate garbage? (I doubt that too...) Does the code above free the memory it reserves as expected to your machine? Do you have anything to propose to me about my peculiar situation? Please don't start telling about the stack, heap, and how they work. I'm well aware of all of that, theoretically. It just doesn't work exactly like that in practice. Thank you in advance, and sorry if my English is not perfect (not my native tongue).
Upvotes: 2
Views: 103
Reputation: 10415
'delete' does not give the memory back to windows. So windows tools do not show what is happening. Your runtime library (a part of your process, not part of the operating system) keeps that memory for possible reuse on future 'new' requests.
Upvotes: 3
Reputation: 182743
You've asked a lot of mostly unrelated questions. Let me try to clear up your main issue:
The process has memory management code that makes intelligent decisions about when to return virtual memory to the operating system and when to keep it around in case it's needed later. Since virtual memory (address space) is not normally considered a scarce resource, there's no particular reason you should want the process to release it. The operating system puts physical memory (RAM, the stuff that's precious) to the best use it can at all times, giving it to and taking it from processes as it thinks best, no matter what the process does with its allocated virtual memory.
Upvotes: 4