Does an Application memory leak cause an Operating

2020-05-30 01:49发布

When we say a program leaks memory, say a new without a delete in c++, does it really leak? I mean, when the program ends, is that memory still allocated to some non-running program and can't be used, or does the OS know what memory was requested by each program, and release it when the program ends? If I run that program a lot of times, will I run out of memory?

9条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-30 02:06

No. Once the OS finishes closing the program, the memory comes back (given a reasonably modern OS). The problem is with long-running processes.

查看更多
太酷不给撩
3楼-- · 2020-05-30 02:13

Leaked memory is returned by the OS after the execution has stopped.

That's why it isn't always a big problem with desktop applications, but its a big problem with servers and services (they tend to run long times.).

Lets look at the following scenario:

  1. Program A ask memory from the OS
  2. The OS marks the block X as been used by A and returns it to the program.
  3. The program should have a pointer to X.
  4. The program returns the memory.
  5. The OS marks the block as free. Using the block now results in a access violation.
  6. Program A ends and all memory used by A is marked unused.

Nothing wrong with that.

But if the memory is allocated in a loop and the delete is forgotten, you run into real problems:

  1. Program A ask memory from the OS
  2. The OS marks the block X as been used by A and returns it to the program.
  3. The program should have a pointer to X.
  4. Goto 1

If the OS runs out of memory, the program probably will crash.

查看更多
霸刀☆藐视天下
4楼-- · 2020-05-30 02:15

Memory leaks don't persist past end of execution so a "solution" to any memory leak is to simply end program execution. Obviously this is more of an issue on certain types of software. Having a database server which needs to go offline every 8 hours due to memory leaks is more of an issue than a video game which needs to be restarted after 8 hours of continual play.

The term "leak" refers to the fact that over time memory consumption will grow without any increased benefit. The "leaked" memory is memory neither used by the program nor usable by the OS (and other programs).

Sadly memory leaks are very common in unmanaged code. I have had firefox running for a couple days now and memory usage is 424MB despite only having 4 tabs open. If I closed firefox and re-opened the same tabs memory usage would likely be <100MB. Thus 300+ MB has "leaked".

查看更多
萌系小妹纸
5楼-- · 2020-05-30 02:18

As far as I know, on most OS when a program is started it receives a defined segment of memory which will be completely liberated once the program is ended.

Memory leaks are one of the main reason why garbage collector algorithms were invented since, once plugged into the runtime, they become responsible in reclaiming the memory that is no longer accessible by a program.

查看更多
够拽才男人
6楼-- · 2020-05-30 02:19

No, in all practical operating systems, when a program exits, all its resources are reclaimed by the OS. Memory leaks become a more serious issue in programs that might continue running for an extended time and/or functions that may be called often from the same program.

查看更多
女痞
7楼-- · 2020-05-30 02:24

When the process ends, the memory gets cleared as well. The problem is that if a program leaks memory, it will requests more and more of the OS to run, and can possibly crash the OS.

查看更多
登录 后发表回答