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.
-
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.