Sherif
Sherif

Reputation: 1279

What happens if I didn't call delete operator after allocating data using new and ending program?

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.

Upvotes: 5

Views: 548

Answers (6)

Karmastan
Karmastan

Reputation: 5706

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.

Upvotes: 2

Loki Astari
Loki Astari

Reputation: 264621

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.

Upvotes: 0

Paul Beckingham
Paul Beckingham

Reputation: 14915

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.

Upvotes: 2

Alok Save
Alok Save

Reputation: 206576

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.

Upvotes: 1

Pete Wilson
Pete Wilson

Reputation: 8694

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.

Upvotes: 1

cnicutar
cnicutar

Reputation: 182674

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

Upvotes: 5

Related Questions