简体   繁体   中英

Memory management in C++ mixed mode application

I'v a mixed mode application which is x32. So there only 2G of memory available. The application processes some large data and allocates about 1.5G in unmanaged heap. Then it releases allocated unmanaged memory without leaks. But the next step is to process about 1.5G in managed mode. And the application crashed when trying to add element in List when about 200M of managed memory allocated. As I suppose, the unmanaged heap manager grabs memory for 1.5G, allocates objects in it, then deallocated objects but don't release heap memory to be accessable to managed heap manager. How managed and unmanaged memory managers share process's memory? How can I tackle with such problem?

Here a sample code which throws exception when trying to allocate managed memory after allocating and releasing unmanaged code. It must be compiled in x32. Why does this happen?

            int size = 1024 * 1024 * 1024 / 2 / 10;

            char* * cppArray = new char*[size];

            for(int i = 0; i < size - 1; i++)
            {
                char *str = (char*)malloc(10 * sizeof(char));
                strcpy(str, "AAAAAAAAAA"); 
                cppArray[i] = str;
            }

            for(int i = 0; i < size - 1; i++)
            {
                char* str = cppArray[i];
                free(str);
            }

            delete[] cppArray;

            List<String^>^ pArray = gcnew List<String^>();

            size = 1024 * 1024 * 1024 / 2 / 7 / 2 / 2;

            for(int i = 0; i < size - 1; i++)
            {
                pArray->Add(gcnew String("AAAAAAAAAA" + i.ToString()));
            }

Thank you.

For the first part, whatever is once malloc'ed stays mallaoc'ed. The reason is the way malloc() gets its memory from the OS, namely increasing the heap max address. So to release it again you have to be sure there are no more references to any address there or you get a segment fault.

Now there is a way around this that might work namely mmap() find an allocator that use mmap() , or whatever its called in MS speak. Make sure it can release it again with something like munmap() .

Allocate your data in the memmapped addresses and be sure no reference to it is left before you unmap it.

ps. Also like @JSF said compile with LargeAddressAware and enable 3GB on your OS. And be sure you have at least 4GB RAM even on 32 bit systems, the OS uses some of it that is not accessible to users. Even if each program only have access to 3GB that still add up to more than 3GB really fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM