The Inline C code below queries and displays the global memory stats (in 4096 byte pages) on my system.

It then allocates and frees successively larger chunks of virtual ram until the attempt fails.

The output looks like this:

c:\test>valloc Global Memory Status Total Physical:392956 pages Available Physical:309695 pages ## 1.5 GB physical with ~1.3 GB free Total PageFile:1048575 pages Available PageFile:1048575 pages ## 4 GB of pagefile all free Total Virtual :524256 pages Available Virtual :516124 pages ## 2 GB of Virtual Memory Available mostly free Allocated: 260000 pages [1064960000 bytes] at 280d0000 Allocated: 270000 pages [1105920000 bytes] at 280d0000 Allocated: 280000 pages [1146880000 bytes] at 280d0000 Allocated: 290000 pages [1187840000 bytes] at 280d0000 Allocated: 300000 pages [1228800000 bytes] at 280d0000 Allocated: 310000 pages [1269760000 bytes] at 280d0000 Allocated: 320000 pages [1310720000 bytes] at 280d0000 Not enough storage is available to process this command at c:\test\valloc.pl line 18, <STDIN> line 7.

Note the close correspondance between the 320,000 pages successfully allocated and the 309,695/392,956 physical memory available. This is not a coincidence. (YMMV on other OSs).

#! perl -slw use strict; #use Inline 'FORCE'; use Inline C => 'DATA', NAME => 'valloc', CLEAN_AFTER_BUILD => 0; use constant PAGE => 4096; my( $tPhys, $aPhys, $tPage, $aPage, $tVirt, $aVirt ) = globalMemoryStatus(); print <<FMT; Global Memory Status Total Physical:$tPhys pages\tAvailable Physical:$aPhys pages Total PageFile:$tPage pages\tAvailable PageFile:$aPage pages Total Virtual :$tVirt pages\tAvailable Virtual :$aVirt pages FMT for my $nPages ( map $_ * 10000, 26 .. 52 ) { my $alloc = $nPages * PAGE; my $addr = virtualAlloc( $alloc ) or die $^E; printf "Allocated: $nPages pages [$alloc bytes] at %x", $addr; <ST +DIN>; virtualFree( $addr ) or die $^E; } __DATA__ __C__ #include <windows.h> void globalMemoryStatus ( ) { Inline_Stack_Vars; MEMORYSTATUS stat; GlobalMemoryStatus( &stat ); Inline_Stack_Reset; Inline_Stack_Push( sv_2mortal( newSVuv( stat.dwTotalPhys / 4096 ) +) ); Inline_Stack_Push( sv_2mortal( newSVuv( stat.dwAvailPhys / 4096 ) +) ); Inline_Stack_Push( sv_2mortal( newSVuv( stat.dwTotalPageFile / 409 +6 ) ) ); Inline_Stack_Push( sv_2mortal( newSVuv( stat.dwAvailPageFile / 409 +6 ) ) ); Inline_Stack_Push( sv_2mortal( newSVuv( stat.dwTotalVirtual / 4096 + ) ) ); Inline_Stack_Push( sv_2mortal( newSVuv( stat.dwAvailVirtual / 4096 + ) ) ); Inline_Stack_Done; return; } U32 virtualAlloc( U32 size ) { return (U32)VirtualAlloc( NULL, (SIZE_T)size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE ); } U32 virtualFree( U32 a ) { return VirtualFree( (LPVOID)a, 0, MEM_RELEASE ); }

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^6: Reading huge file content by BrowserUk
in thread Reading huge file content by siva kumar

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.