Pogolin

Hi all,

Recently, I have encountered a problem and I tried to debug it. I found that system throw out a CMemoryException when I try to allocate 256MB memory.

I have a member function in one class . see the below list.

ClassA:ClassA()

{

...

mImage = NULL;

...

}

ClassA:~ClassA()

{

delete []mImage;

}

bool ClassA : FuncA()

{

...
try
{
unsigned long* mImage = new unsigned long[67108864]; // 64M*4 = 256MByte
}
catch( CMemoryException* pEX)
{
CString cstmp;
CString csOut;
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);

cstmp.Format("The MemoryStatus structure is %ld bytes long.\n",stat.dwLength);
csOut+=cstmp;
cstmp.Format("It should be %d.\n", sizeof (stat));
csOut+=cstmp;
cstmp.Format("%ld percent of memory is in use.\n",stat.dwMemoryLoad);
csOut+=cstmp;
cstmp.Format("There are %7d total Kbytes of physical memory.\n",stat.dwTotalPhys/ 1024);
csOut+=cstmp;
cstmp.Format("There are %7d free Kbytes of physical memory.\n",stat.dwAvailPhys/1024);
csOut+=cstmp;
cstmp.Format ("There are %7d total Kbytes of paging file.\n",stat.dwTotalPageFile/1024);
csOut+=cstmp;
cstmp.Format("There are %7d free Kbytes of paging file.\n",stat.dwAvailPageFile /1024);
csOut+=cstmp;
cstmp.Format("There are %7x total Kbytes of virtual memory.\n",stat.dwTotalVirtual/1024);
csOut+=cstmp;
cstmp.Format("There are %7x free Kbytes of virtual memory.\n",stat.dwAvailVirtual/1024);
csOut+=cstmp;
pEx->Delete();
AfxMessageBox(csOut);
}
...

}

When I called FuncA() to allocate memory and if I called another function beforre, system will throw out CMemoryException.

But if I called FuncA() to allocate memory and I don't called another function, sysem will not throw out CMemoryException.

When system throw out CMemoryException, I tried to print out the usage status of memory.

The result is as below

The MemoryStatus structure is 32 bytes long.
It should be 32.
36 percent of memory is in use.
There are 2096100 total Kbytes of physical memory.
There are 1321288 free Kbytes of physical memory.
There are 4041780 total Kbytes of paging file.
There are 3615048 free Kbytes of paging file.
There are 1fff80 total Kbytes of virtual memory.
There are 70e94 free Kbytes of virtual memory.
I think system has enough memory but I can't allocate successfully.Why Could anybody give me some suggestions Just like adjust some parameters for complier or OS.
Regards,
Pogo



Re: Visual C++ General [Help] Out of Memory

Micha? Zalewski

If you allocate memory with new operator system must allocate linear part of memory. Maybe you have fragmented it and system can't allocate this amount of memory. You must use smaller tables if you want to allocate 256MB.





Re: Visual C++ General [Help] Out of Memory

Pogolin

Sorry, may I ask you what is smaller tables

I need a continued memory, because I will use this memory as array to save image.

I can't allocate 256MB composed of serveral smaller memory like 8M,16M.

I don't know whether you can know what I mean.

Do you know how to solve fragmented problem






Re: Visual C++ General [Help] Out of Memory

Micha? Zalewski

I know what you want to do. I think that you can use several tables in class which have a overload operator[] and it can manipulate tables. But don't how you can use continued memory




Re: Visual C++ General [Help] Out of Memory

Simple Samples

Pogolin wrote:
Do you know how to solve fragmented problem

Do as Micha said; use smaller peices of memory. You can do it if you understand you must.

As an old programmer with experience when memory was very expensive, I was amazed when I heard about a PC with 16 MB of memory. That was huge for the time, but that was a time when computer-aided (graphical) design was old technology. Big jets such as the 747 and the L-1011 were designed with computers using less memeory. Developers these days are spoiled compared to a decade or more in the past. I hope that helps you to understand why other developers might not be as helpful as you want them to be.

Certainly it is possible to spend too much time trying to optimize use of memory. Hardware is cheap enough that our time is most expensive, but sometimes we must spend a little time to overcome limitations.






Re: Visual C++ General [Help] Out of Memory

Pogolin

Thanks for Michal's suggestion and Samples's comment. I'd modified the usage of memory from big continued memory to several pieces of smaller memory. It is new version of this application. But the performance of application will down very seriously. Because this big memory will be access very frequently, if I divided them into several pieces, system would spend additioal time to determine which pieces will be use and this procedure will make system running slowly.

My quesiton is that system still has enough memory but why I can't use it System still has 1.3G, why I can't allocate a 256MB continued memory






Re: Visual C++ General [Help] Out of Memory

einaros

The amount of physical ram available on your system doesn't matter. All applications will (under normal circumstances) get a virtual address space of 2 GB. These 2 GB will hold the application itself will be loaded; all dlls the app implicitly or explicitly depends on; all allocations made by the app or the dlls. The amount of memory you are able to allocate within this therefore depends on how large your application is, how many dlls are loaded, how much memory has been already allocated, and finally: how much continuous memory is available all factors considered. If the free memory within the virtual address space is badly fragmented, large allocations will no longer be possible.





Re: Visual C++ General [Help] Out of Memory

Simple Samples

Are you saying you have determined that your program executes faster if you use one large peice compared to smaller peices If so then that means you have been able to get your program to work with one large 256 MB peice of memory. Have you If not then you can't be sure the program would be noticeably faster or slower.

Is your program not executing fast enough Do you know if it is thrashing If it is thrashing then that will affect performance much more than a little bit of processing time spent determining what peices to use. Do you know what the percent of CPU utilization is If the percent of CPU utilization is not maximum, then the little bit of processing time spent determining what peices to use is not the problem.






Re: Visual C++ General [Help] Out of Memory

Pogolin

To einaros,

OK~ I know what you mean. But this function can run successfully sometimes. That's why I'm confused that system can allocate 256MB sometimes but sometimes can't.

What kind of procedure will cause memory fragmented Do you know what tool can display the distribution of memory

Thanks for your reply~

Regards,

Pogo






Re: Visual C++ General [Help] Out of Memory

Pogolin

To Simples,

Yes, use smaller peices is slower than larger peice.

Yes, sometimes it can works with 256MB, sometimes can't. Not always happens "Out of memory"

I will use that pointer like below list.

int RowLong = 2048;

int Height = 32768;

unsigned long lMask[32] = { 0x00000080, 0x00000040, 0x00000020, 0x00000010,
0x00000008, 0x00000004, 0x00000002, 0x00000001,
0x00008000, 0x00004000, 0x00002000, 0x00001000,
0x00000800, 0x00000400, 0x00000200, 0x00000100,
0x00800000, 0x00400000, 0x00200000, 0x00100000,
0x00080000, 0x00040000, 0x00020000, 0x00010000,
0x80000000, 0x40000000, 0x20000000, 0x10000000,
0x08000000, 0x04000000, 0x02000000, 0x01000000};

for( int y= 0 ; y < Height ; y++)

{

for( int row = 0 ; row < RowLong ; row++ )

{

for( int x= 0 ; x < 32 ; x ++)

{

int idx = y*RowLong + row;

mImage[idx] |= lMask[x];

}

}

}

If I divided into several pieces, then I need to determine which pieces will be used.

The pointer is accessed very frequently. The performance will down.

Do you know what I mean Thanks for your comment.

Regards,

Pogo