Memory allocation problem

3 views
Skip to first unread message

Gildo

unread,
Mar 20, 2007, 6:30:51 AM3/20/07
to
I've a problem to allocate a large buffer: 600 MegaB size.

To make this operation i use an easy program i put at the end of the
current post.

the test i made are:
1. PC with 2GigaByte of memory RAM - OS: windows XP
a) if i try to allocate 600 (six hundred) MegaByte it's all ok
b) if i try to allocate 700 (seven hundred) MegaByte the system failed.

2. PC with 512MegaByte of memory RAM - OS: windows XP
a) if i try to allocate 600 (six hundred) MegaByte it's all ok
b) if i try to allocate 700 (seven hundred) MegaByte it's all OK
c) if i try to allocate 1055 (one thousand and fiftyfive) MegaByte
it's all OK
d) if i try to allocate 1056 (one thousand and fiftysix) MegaByte
the system failed
To explain the difference between case c) and case d) is that the
NoPagin Parameter on TaskManager Performance tab is 10556 (ten thousand
five hundred and fiftysix) KB, is this an accident?


The final question is:

is there some settings on window regsitry that can allow the program on
PC1 to allocate more than 600 MegaBytes?


The easy test program follows below:

// ------------------------------------
// Name: testmem.cpp

// to compile and link this program i use the borland c++ builder 5
// compilerm in particulare the command is:
// "bcc32 -WR testmem noeh32.lib"

#include <windows.h>
#include <cstring>

void WINAPI MBox(const char *msg, const char *title = "")
{
MessageBox(HWND_DESKTOP, msg, title, MB_OK);
}


DWORD WINAPI ShowLastError(const char *title_text)
{
TCHAR *err_msg;
DWORD err_value = GetLastError();

if (err_value != NO_ERROR)
{
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err_value,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&err_msg),
0,
NULL);

const char *title =
(title_text && *title_text) ? title_text : "Error";

MBox(err_msg, title);
LocalFree(err_msg);
SetLastError(0);
}

return err_value;
}


void ShowMemoryStatus()
{
MEMORYSTATUS ms;
static char buffer[256];
// buffer is made static only to remove any
// changing effect on GlobalMemoryStatus calls

GlobalMemoryStatus(&ms);
wsprintf(buffer,
"Memory Load %u%%\r\n"
"Physical Available %u\r\n"
"Page File Total %u\r\n"
"Page File Available %u\r\n"
"Virtual Total %u\r\n"
"Virtual Available %u",
ms.dwMemoryLoad,
ms.dwTotalPhys,
ms.dwTotalPageFile,
ms.dwAvailPageFile,
ms.dwTotalVirtual,
ms.dwAvailVirtual);
MBox(buffer, "Global Memory Status");
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
ShowMemoryStatus();

HGLOBAL hg = GlobalAlloc(GMEM_FIXED, 700 * 1024U * 1024U);

if (hg)
{
MBox("Allocation Succeeded");
GlobalFree(hg);
}
else
{
ShowLastError("Allocation Failed");
}

return 0;
}

// ------------------------------------

Scott McPhillips [MVP]

unread,
Mar 20, 2007, 9:29:59 AM3/20/07
to
Gildo wrote:
> I've a problem to allocate a large buffer: 600 MegaB size.
>
> To make this operation i use an easy program i put at the end of the
> current post.
>
> the test i made are:
> 1. PC with 2GigaByte of memory RAM - OS: windows XP
> a) if i try to allocate 600 (six hundred) MegaByte it's all ok
> b) if i try to allocate 700 (seven hundred) MegaByte the system failed.
>
> 2. PC with 512MegaByte of memory RAM - OS: windows XP
> a) if i try to allocate 600 (six hundred) MegaByte it's all ok
> b) if i try to allocate 700 (seven hundred) MegaByte it's all OK
> c) if i try to allocate 1055 (one thousand and fiftyfive) MegaByte
> it's all OK
> d) if i try to allocate 1056 (one thousand and fiftysix) MegaByte the
> system failed
> To explain the difference between case c) and case d) is that the
> NoPagin Parameter on TaskManager Performance tab is 10556 (ten thousand
> five hundred and fiftysix) KB, is this an accident?
>
>
> The final question is:
>
> is there some settings on window regsitry that can allow the program on
> PC1 to allocate more than 600 MegaBytes?

The amount of memory you can allocate has nothing to do with the amount
of RAM installed. All memory you can allocate is virtual. Whatever
doesn't fit in RAM is "allocated" in the page file. The difference
between your two machines is probably a difference in the page file
setting, which is in Control Panel, System, Advanced.

--
Scott McPhillips [VC++ MVP]

Gildo

unread,
Mar 20, 2007, 10:42:53 AM3/20/07
to
Scott McPhillips [MVP] ha scritto:

>
> The amount of memory you can allocate has nothing to do with the amount
> of RAM installed. All memory you can allocate is virtual. Whatever
> doesn't fit in RAM is "allocated" in the page file. The difference
> between your two machines is probably a difference in the page file
> setting, which is in Control Panel, System, Advanced.
>

Page file settings are

1. Pc with 2GByte
. start dimension 3070 MB
. maximum dimension 4092 MB

2. Pc with 512MB
. start dimensione 768 MB
. maximum dimension 1536 MB

I checked the others settings on System Advanced Tab but i didn't see
any interesting differences!

do you have some other tips?


Many thanks anyway.

Scott McPhillips [MVP]

unread,
Mar 20, 2007, 11:10:12 AM3/20/07
to
Gildo wrote:
> Page file settings are
>
> 1. Pc with 2GByte
> . start dimension 3070 MB
> . maximum dimension 4092 MB
>
> 2. Pc with 512MB
> . start dimensione 768 MB
> . maximum dimension 1536 MB
>
> I checked the others settings on System Advanced Tab but i didn't see
> any interesting differences!
>
> do you have some other tips?

Sure. The other big problem with allocating large blocks is memory
space fragmentation. You might have 1000 Mbytes available but not all
in one contiguous block. So several smaller allocations will probably
get you more memory than asking for one large block.

Gildo

unread,
Mar 20, 2007, 11:48:41 AM3/20/07
to
Scott McPhillips [MVP] ha scritto:

OK, i understand what you say but i belive (it' my impression) that the
difference between two PCs is caused by different settings!

About memory space fragmentation i noticed that some times the test
program also fail with 600 MByte buffer not only with 700 MByte;
For this behaviour i can think that there is a problem of contiguous block.

The processes run on PC with 2GB can't to allocate 1GByte of memory.

Other tips are welcomed.

Thanks

Stephen Kellett

unread,
Mar 20, 2007, 3:07:55 PM3/20/07
to
In message <46000258$0$4787$4faf...@reader4.news.tin.it>, Gildo
<hdhfghgfd...@tin.it> writes

>OK, i understand what you say but i belive (it' my impression) that the
>difference between two PCs is caused by different settings!

You can check that theory by running VM Validator and looking at the
Virtual View to show you the virtual memory usage of your test
application on each computer. I think you'll find the difference is to
do with what is installed on each PC and what gets loaded into your
app's address space.

Find out more at
http://www.softwareverify.com/cpp/virtual_memory/index.html

VM Validator is free to download.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk/software.html
Computer Consultancy, Software Development
Windows C++, Java, Assembler, Performance Analysis, Troubleshooting
Reg Office: 24 Windmill Walk, Sutton, Ely, Cambs CB6 2NH.

Gildo

unread,
Mar 21, 2007, 3:03:20 AM3/21/07
to
Hi Stephen,

first of all, thanks for your tip.

i've just downloaded the VM Validator and seeing the help file i noticed
in "What do you need to run VM Validator?" paragraph that "VM
Validator requires your application to be built using Microsoft® Visual
Studio® 6.0 service pack 3 or later."

I'm working with borland c++ builder 5 compiler do you know this one is
the same with MVS compiler?

Thanks again.

Gildo

unread,
Mar 21, 2007, 5:24:40 AM3/21/07
to
Hi Stephen,

i tried VM Validator and the thesis about "continuous block" is
partially right.

I say partially because

a) from one side i see the memory fragmentation and i can know the size
of continuous memory block

b) on the other side i see on the PC with 512MB of memory RAM 1GB of
virtual memory ram while on the PC with 2GB of memory RAM i see "only"
2GByte of Virtual memory!

It seems that on PC with 2GB of memory ram the settings about Virtual
Memory on System\Advanced\Virtual Memory has not been apply!

The only interesting difference between two PC is that the PC work
properly has NTFS file system while the PC not work properly has FAT32
file system.

Now my question is:

can i move the "pagefile.sys" file to a partition, that i have, with
NTFS file system?

Many thanks

Gildo

unread,
Mar 21, 2007, 8:32:09 AM3/21/07
to
Gildo ha scritto:

>
> can i move the "pagefile.sys" file to a partition, that i have, with
> NTFS file system?
>

I've moved the "pagefile.sys" file to a NTFS partition but VM Validator
still see only 2(two)GB of Virtual memory and not 3(three)GB how by
settings.

what parameter do i have to set so that VM Validatore see 3(three)GB of
Virtual Memory?

Many thanks


Reply all
Reply to author
Forward
0 new messages