开发者

Error in a Wikipedia Article on memory leaks [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 9 years ago.

Improve this question

At the end of this wikipedia article, in a section "A simple example in C" it is claimed that "the operating system delays real memory allocation until something is written into it". From my experience this is not possible or better put, "not feasible". An OS/Processor does not track each and every memory write. Is the wikipedia article wrong like I suspect ? If not enlighten me.

edit

I guess when one takes page faults into consideration, than it does make sense in some ways. However, page faults and virtual memory don't fit well with a discussion on memory leaks. I mean is if process 开发者_如何学JAVAallocates 2 pages, writes to one (page one), and then leaks both pages it's still a leak regardless of weather the second page leaks physical memory or not. I guess the section should differentiate between physical memory and virtual memory ? :D


Depends on the operating system. On Linux, for example, it actually does work like that. Look up "optimistic memory allocation".

In addition to that, there is the differentiation between virtual memory and physical memory. On most operating systems, you are using virtual memory, so even though you allocate many megabytes worth of memory, the actual physical memory will only be tapped once you write into it (at which point what had been at that location before might get paged back to disk).

There is the "virtual address space" which is basically just "logical" memory that is then later mapped to the physical RAM (or your hard drive, if the memory has been paged out).


You're mistaken.

All the OS has to allocate is virtual memory. You call, say, mmap or break. It creates some virtual address space, and sets the page table entries to cause page faults. You get around to referencing the memory, the page fault happens, and the system goes to do a real memory allocation. Not before.


I would say the comment is at the very least irrelevant to memory leaks, even if it's not erroneous. There are at least 3 measurements of memory usage:

  • virtual address space consumed
  • commit charge
  • physical memory occupied

Even if memory allocated has not been physically instantiated by a page fault, it's still taking up valuable virtual address space and commit charge.


Lets say you allocate 1GB of virtual memory and actually only reference and use 0.5GB you still have leaked 0.5GB of your allocated address space. Even if it does not occupy actual real RAM it still requires resources from the memory allocator to manage your allocations (especially when you have allocated lots of small objects). So it still is a memory leak, because (at least on 32bit) you can only allocate an additional 1GB max. instead of 1.5GB and you have no way of releasing it from within your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜