Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.

In reply to Re^5: RFC: Abusing "virtual" memory (page faults vs malloc) by BrowserUk
in thread RFC: Abusing "virtual" memory by sundialsvc4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (20)
    As of 2014-08-27 19:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (250 votes), past polls