MFC: how to give my application more memory

Posted on 2011-04-20
Last Modified: 2012-05-11
Dear experts,

I have an MFC application. A CMemoryException ("Not enough memory") is thrown at this line:
short* MonoBuff = new short[iSize];
iSize is 191,574,136.
Some time ago this application used to work, but many changes were made since then, so I don't know how to find what caused the problem. If iSize is 180,000,000, the exception is not thrown. Could you tell me please how I can give my application more memory, it is possible at all?
Question by:tantormedia
    LVL 33

    Accepted Solution

    Install additional memory into your system is the first thing that comes to mind. Reconsider if it's really necessary to allocate 190 million short's at one shot is the next thing that comes to mind. And finally, while that seem excessive to me, I would kind of expect Windows should be able to pull that off with the help of swap, but perhaps you may find success using a private heap:
    LVL 9

    Expert Comment

    Memory allocation success depends not only on the size of the request but also on the fragmentation of available memory. e.g. there might be 20 blocks of 9,000,000 bytes available but if they are scattered instead of being contiguous, your request would fail. Other reason is that you are truly out of memory.

    From the limited information posted here, it is not possible to diagnose the problem.

    1. Is this 32-bit or 64-bit application? 64-bit apps should not run out of memory.
    2. Catch the exception, create breakpoint in catch. When the exception is caught and program pauses, open task manager and check memory usage. Is enough memory available?
    3. Does it fail on first allocation or does it go through a few times and then fail?
    LVL 86

    Expert Comment

    Installing more memory won't help, a 32bit app always has only 2GB tops of address space available for allocation. You can try to determine the largest chunk of available memory before the allocation and not allocate if the size isn't sufficiant, e.g.
    UINT unSizeAvail = HeapCompact(GetProcessHeap(),0); // will also perform some 'cleanup'
    if (unSizeAvail > (iSize * sizeof(short) {
      short* MonoBuff = new short[iSize]; 

    Open in new window


    Author Comment


    I tried it, and unSizeAvail is only 278,528. It cannot even be compared to 190,000,000 * 2, and yet the program didn't throw any exceptions when iSize is 180,000,000. I cannot explain it.


    Author Comment


    It is 32 bit application.
    Enough memory is available on the machine.
    It fails on first allocation of that size.

    Author Comment


    Is it really possible to use private heap with MFC?

    LVL 33

    Expert Comment

    by:Todd Gerbert
    Yes, you can use a private heap. You'll need to make sure you handle the lifetime of the heap - i.e. in addition to the short* you'll need somewhere to store the handle to the heap.

    int count = 191574136;
    int sizeRequired = count * sizeof(short);
    // Create heap
    HANDLE hHeap = HeapCreate(0, 0, 0); // initial size 0, with max size = 0 it will grow
    if (hHeap)
    	short *pMonoBuffer = NULL;
    	// Allocate memory on private heap
    	pMonoBuffer = (short *)HeapAlloc(hHeap, 0, sizeRequired);
    	if (pMonoBuffer)
    		// use the array
    		short val = -32768;
    		for (int i = 0; i < count; i++)
    			pMonoBuffer[i] = val++;
    		// When done free the memory
    		HeapFree(hHeap, 0, pMonoBuffer);
    		// and destroy the heap

    Open in new window

    To be honest, I'm not sure why this would work and not your current method - it's a bit of a shot in the dark.  You might also consider setting the /3GB switch in boot.ini (on a 32-bit system this'll get you a 3GB addressable memory space).
    LVL 32

    Expert Comment

    private heap cannot be a solution. if you don't get 400 mb contiguous memory for an array from operation system why it should give you same amount or more for a private heap?

    two ideas:

    you might reserve the memory when the application was started. to this time you should get bigger chunks. so try in constructor of your application class to get the memory by

    m_pMonoBuff = new short int[200000000];
    assert(m_pMonoBuff != NULL);

    Open in new window

    where m_pMonoBuff is a member of your app class.

    if that fails also (what i would expect) you should allocate smaller chunks like with

    std::vector<std::vector<short int> > monoBuffVec(65536, std::vector<short int>(4096, 0));

    Open in new window

    what would give 64k * 4k shorts.

    to access index idx you would do

    short int si_at_idx = monoBuffVec[idx>>12][idx&0x0fff];

    Open in new window



    Author Closing Comment

    Thank you all who tried to help.
    I decided to allocate the memory in pieces of 1,000,000 each and use them in a loop, freeing after each iteration. It worked.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now