5

What happens if I didn't call delete operator after allocating data using new. I know that the data that has been allocatted, won't be available until releasing it, but after ending the program ?

Why the PC seems to have nausea xD, i mean that it is very slow but after a while its performance become better but not like before the program execution ?

Note: I'm running windows XP.

gnat
  • 6,213
  • 108
  • 53
  • 73
Sherif
  • 1,249
  • 4
  • 15
  • 38
  • Can you say more about what you are doing with the PC where you notice poor performance after your test program ends? – quamrana Apr 22 '11 at 13:57
  • Nothing specific, open control panel, explorer, or launching another program, all tasks required more time than ever. – Sherif Apr 22 '11 at 14:17
  • define `nausea`. How do you measure `slow`. The memory itself is not a problem this will be re-claimed. Its the other resources that are held because object destructors are not called to tidily close these resources (example sockets may re-main unusable for a minute or two if not correctly closed). – Martin York Apr 22 '11 at 14:24
  • possible duplicate of [What REALLY happens when you don't free after malloc?](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) – Jason C Mar 25 '15 at 05:39

6 Answers6

5

When the program ends all the memory it requested (stack, heap whatever) is claimed by the operating system.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Why the PC seems to have nausea xD, i mean that it is very slow but after a while its performance become better but not like before the program execution – Sherif Apr 22 '11 at 13:18
  • 2
    @mavric I guess it depends on the resource the process used. For instance, if the process used lots and lots of memory, it probably forced the OS to swap out a lot of stuff. Swapping back in takes time. **This is just speculation** – cnicutar Apr 22 '11 at 13:22
  • Actually, I made a program to intentially leak memory, just for education purposes. So I make it allocate and don't release a huge memory (the page file usage was jumping to its maxmimmum). anyway, I tried to wait if by time it goes better but it doesn't... – Sherif Apr 22 '11 at 13:42
2

Suppose you wrote your own version of the 'ls' command, and the memory management was so poor that it leaked 10 MB every time it ran, which is a large leak. Does this matter? Not really. The system will reclaim all memory when the program exits, which is most likely a fraction of a second after it starts. Sure, your pride is affected, and the craftsmanship would be low, but the system doesn't suffer. It is likely the user would never know how poorly the memory is managed.

Now suppose you wrote your own version of Apache. It is expected to run for months at a time without restart, so even if it leaked a small amount of memory, that would accumulate over time and cause a problem. The user would likely know about this one. The sysadmins certainly would.

So to summarize, the OS does the right thing and reclaims memory. Leaky software is bad. But there are cases where it doesn't matter much.

Paul Beckingham
  • 14,495
  • 5
  • 33
  • 67
  • Actually, I made a program to intentially leak memory, just for education purposes. after i close the program the task manager show that the memory has been reurned back to the OS, but the system performance doesn't go back as before the execution of the program. i can't find why does this happen ?!! – Sherif Apr 22 '11 at 13:40
  • How much did you leak? Performance is a vague term - could you be specific? – Paul Beckingham Apr 22 '11 at 14:47
  • while(i<9){ double* arr = new double[300000000]; i++; } actually the pc was allocating the first row then stuck. – Sherif Apr 22 '11 at 14:55
  • How much memory has your PC got? I think that a double is at least 8 bytes, so you're allocating .. >2GB? each iteration? – quamrana Apr 22 '11 at 15:23
  • 1 GB of Ram. beside the virtual memory it was jumping to 2.03 GB. – Sherif Apr 22 '11 at 21:00
2

I think you're seeing the effects of evicting useful programs from main memory to the disk.

Your intentionally-leaky program is trying to allocate all memory on the system. To satisfy your program's demand, Windows is finding other programs on the system, writing their memory to the page file, and reallocating their memory to your program. When you see the page file usage jump to maximum, it's because most other programs have been shoved there instead of main memory.

When your program exits, Windows reclaims all the program's memory (as noted by others). But all the other programs on your computer still have their memory saved in the page file on disk, not in main memory. So when they run, Windows has to go load their memory pages from disk, making the program appear slow. After a while, the programs will move back to main memory, and performance will look like normal.

Karmastan
  • 5,618
  • 18
  • 24
1

All the memory your program allocated, but failed to release, is automatically released when your program ends. That is, after your program finishes it no longer has any effect or influence on the OS.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
  • Why the PC seems to have nausea xD, i mean that it is very slow but after a while its performance become better but not like before the program execution – Sherif Apr 22 '11 at 13:19
1

The heap and the stack memory are allocated to each process. Dynamic allocations happen on the heap. Each executable or program runs in a process. So once the program ends the heap and the stack allocated for that process are returned back to the OS. This includes the leaking dynamic memory.
Once the program has ended, It should have no bearing on performance of the OS.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Why the PC seems to have nausea xD, i mean that it is very slow but after a while its performance become better but not like before the program execution – Sherif Apr 22 '11 at 13:18
0

The memory itself is not a problem this will be re-claimed automatically.

Its the other resources that are held because object destructors are not called to tidily (because you did not delete the memory the destructor(s) are not called). close these resources.

Simple examples are:

  • sockets may re-main unusable for a minute or two if not correctly closed.
  • DB handles could cause the DB to use up all its connections.
  • Space station spins out of orbit because of static on the line.
Martin York
  • 257,169
  • 86
  • 333
  • 562