<?xml version="1.0" encoding="windows-1252"?>
<node id="653503" title="Re^5: RFC: Abusing &quot;virtual&quot; memory (page faults vs malloc)" created="2007-11-28 05:01:43" updated="2007-11-28 00:01:43">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;This displays the page fault count before and after allocating an 40960 byte chunk of memory from a heap, then freeing it and allocating it again. 

&lt;code&gt;
#! perl -slw
use strict;
use Inline  C =&gt; Config =&gt; LIBS =&gt; '-lpsapi.lib';

use Inline C =&gt; 'DATA', NAME =&gt; 'heap', CLEAN_AFTER_BUILD =&gt; 0;

my $heap = heapCreate( 0, 0, 1024 * 1024 );

my $space = heapAlloc( $heap, 0, 4096 * 10 );
heapFree( $heap, 0, $space );

$space = heapAlloc( $heap, 0, 4096 * 10 );

print heapSize( $heap, 0, $space );


__DATA__
__C__

#include &lt;windows.h&gt;
#include &lt;psapi.h&gt;

U32 heapCreate( U32 flags, U32 initial, int max ) {
    return (U32) HeapCreate( flags, initial, max );
}

U32 heapAlloc( U32 hHeap, U32 flags, U32 size ) {
    U32 pMem;
    PROCESS_MEMORY_COUNTERS pmc;
    pmc.cb = sizeof( PROCESS_MEMORY_COUNTERS );

    GetProcessMemoryInfo( GetCurrentProcess(), 
        &amp;pmc, sizeof( PROCESS_MEMORY_COUNTERS ) 
    );
    printf( "pagefaults before alloc of %d bytes: %d\n", 
        size, pmc.PageFaultCount 
    );

    pMem = (U32)HeapAlloc( (HANDLE)hHeap, flags, (SIZE_T)size );

    GetProcessMemoryInfo( GetCurrentProcess(), 
        &amp;pmc, sizeof( PROCESS_MEMORY_COUNTERS ) 
    );
    printf( "pagefaults after alloc of %d bytes: %d\n",
        size, pmc.PageFaultCount 
    );

    return pMem;
}

U32 heapSize( U32 hHeap, U32 flags, U32 mem ) {
    return (U32)HeapSize( (HANDLE)hHeap, flags, (LPVOID)mem );
}

U32 heapFree( U32 hHeap, U32 flags, U32 mem ) {
    return (U32)HeapFree( (HANDLE)hHeap, flags, (LPVOID)mem );
}
&lt;/code&gt;

&lt;p&gt;The output&lt;code&gt;
c:\test&gt;HeapMem.pl
pagefaults before alloc of 40960 bytes: 952
pagefaults after alloc of 40960 bytes: 964
pagefaults before alloc of 40960 bytes: 964
pagefaults after alloc of 40960 bytes: 964
40960

c:\test&gt;HeapMem.pl
pagefaults before alloc of 40960 bytes: 953
pagefaults after alloc of 40960 bytes: 965
pagefaults before alloc of 40960 bytes: 965
pagefaults after alloc of 40960 bytes: 965
40960

c:\test&gt;HeapMem.pl
pagefaults before alloc of 40960 bytes: 953
pagefaults after alloc of 40960 bytes: 965
pagefaults before alloc of 40960 bytes: 965
pagefaults after alloc of 40960 bytes: 965
40960
&lt;/code&gt;

&lt;p&gt;Shows that the first time, allocating 10 pages of memory results in 12 page faults. The second time none.



&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;In the absence of evidence, opinion is indistinguishable from prejudice.&lt;/div&gt;
&lt;div&gt;[http://news.bbc.co.uk/1/hi/education/6202877.stm|"Too many &amp;#91;&amp;#93; have been sedated by an oppressive environment of political correctness and risk aversion."]&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
653081</field>
<field name="parent_node">
653451</field>
</data>
</node>
