Article View: borland.public.cppbuilder.nativeapi
Article #14832Re: PageFaults... are they a problem??
From: Alan Bellingham
Date: Thu, 31 Jul 2003 14:22
Date: Thu, 31 Jul 2003 14:22
61 lines
3825 bytes
3825 bytes
"MWP" <mwp@overclockers.com.au> wrote: >But i am accessing shared memory from external apps using OpenFileMapping and MapViewOfFile a lot (up to every 5ms). >Would this cause these page faults? Oh, almost definitely yes. When you use these functions, the OS reserves a memory range, and maps that range onto the file. As far as your used code is concerned, that file appears to be in RAM, but it actually isn't. Every 'page' of memory (about 4K in size last time I looked) is noted as being 'not there'. (The processor uses a lookup table for this - for this purpose, it needs 1 bit per 4K of addressable memory, or 128K for 4GB.) When you access a page for the first time, effectively the processor checks the bit. If it says the memory isn't currently mapped, the processor generates a page fault. At this point, Windows catches the exception, and tries to do something about it. In this case, it grabs a page of actual RAM, maps it to the address required, loads the relevant data from the file into that page, flips the bit to now say it's there, and tells the processor to restart the instruction that caused the problem. Oh, yes, "maps it to the address required". The address that the program sees and that the processor actually uses for a page of memory is different. This means another lookup table that the processor uses. Of course, actual RAM isn't infinite, and sooner or later Windows will need the actual RAM for something else. It'll unload the RAM, note the address as being 'not there' again, and use the actual RAM for the something else. (The above ignores the fun and games of what happens when you or something else modifies the mapped file data. If more than one process is mapping the same file, the two different process may look at the same page of actual RAM, but think they're at different addresses.) And now to the conclusion: What can cause a huge number of page faults is mapping a file at least as large as your actual free RAM, and then repeatedly dancing from one end to the other in steps of the page size - as each new chunk of the file is touched, it has to be loaded, only to be discarded before it gets needed again. For this reason, when choosing algorithms to be used with such virtual memory, you want to try to keep them using addresses in the same area as much as possible. Also successfully allocating that sort of size of RAM may do the same thing, since program memory is usually backed by 'virtual memory', which is exactly this sort of file mapping, onto a temporary bit of file. This is also why Windows usually grinds to a halt before actually 'running out of memory'. It spends its time continually loading and discarding virtual memory. Alan Bellingham -- alanb@episys.com
Technical Details
Message-ID:
<538iivgclj4qfo2ui8evdkarffsoqr181h@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3f28f191$1@newsgroups.borland.com> <VA.00001e5c.062c93ff@email.address.sorry> <3f291ea0$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,ec1f290b7afbc30f X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-07-31 07:38:44 PST From: Alan Bellingham <alanb@episys.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: PageFaults... are they a problem?? Date: Thu, 31 Jul 2003 15:22:56 +0100 Reply-To: alanb@episys.com Message-ID: <538iivgclj4qfo2ui8evdkarffsoqr181h@4ax.com> References: <3f28f191$1@newsgroups.borland.com> <VA.00001e5c.062c93ff@email.address.sorry> <3f291ea0$1@newsgroups.borland.com> X-Newsreader: Forte Agent 1.92/32.572 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 193.129.251.130 X-Trace: newsgroups.borland.com 1059661368 193.129.251.130 (31 Jul 2003 07:22:48 -0700) Lines: 61 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail