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.
#! perl -slw
use strict;
use Inline C => Config => LIBS => '-lpsapi.lib';
use Inline C => 'DATA', NAME => 'heap', CLEAN_AFTER_BUILD => 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 <windows.h>
#include <psapi.h>
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(),
&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(),
&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 );
}
The output
c:\test>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>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>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
Shows that the first time, allocating 10 pages of memory results in 12 page faults. The second time none.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.