Click here to Skip to main content

VS 2012/2013 & .NET 4.5.1

 
GeneralRe: processor id Pinmembervivek51117-Feb-13 23:48 
GeneralRe: processor id PinmemberRichard Deeming18-Feb-13 2:05 
AnswerRe: processor id PinmemberMike Meinz18-Feb-13 5:23 
QuestionCrystal Report for Visual Studio 2010 Pinmembersource.compiler14-Feb-13 20:10 
QuestionAdd a Class to a Windows App [modified] PinmemberASPnoob8-Feb-13 18:09 
AnswerRe: Add a Class to a Windows App PinmvpRichard MacCutchan8-Feb-13 22:55 
GeneralRe: Add a Class to a Windows App PinmemberASPnoob8-Feb-13 23:27 
QuestionVirtual memory not released after my program ran out of memory ... [modified] PinmemberMartial8-Feb-13 0:50 
I've developped an application in C++ with Visual studio 10.
My program need to generate a huge quantity of small object (size = 12 Kb)in memory.
I want my program to manage the memory when the process run out of memory.
So I've implement a try/catch bloc to intercept this event. Then I free the memory and I tell the user that the operation is canceled.
But it seems in this case that the virtual memory is released not.
 
here's the code :
// AllocationErr.cpp : définit le point d'entrée pour l'application console.
//

#include "stdafx.h"
//#include <iostream.h> 
#include <vector> 
#include "AllocationErr.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
using namespace std;
 
void ReportMemory()
{
	MEMORYSTATUSEX statex;
 
	statex.dwLength = sizeof (statex);
	GlobalMemoryStatusEx (&statex);
 
	HANDLE heap = GetProcessHeap();
	SIZE_T size =  HeapCompact(heap, 0);
 
	BOOL validated = HeapValidate(heap, 0, NULL);
 
	TRACE("GlobalMemoryStatusEx:\n");
	TRACE(" + TotalVirtual : %0.1lf Mo\n", statex.ullTotalVirtual / (1024.0 * 1024.0));
	TRACE(" + AvailVirtual : %0.1lf Mo\n", statex.ullAvailVirtual / (1024.0 * 1024.0));
	TRACE(" + Process mem load : %0.1lf %%\n", (1.0 - (double) statex.ullAvailVirtual / (double) statex.ullTotalVirtual) * 100.0);
	TRACE(" + TotalPageFile : %0.1lf Mo\n", statex.ullTotalPageFile / (1024.0 * 1024.0));
	TRACE(" + AvailPageFile : %0.1lf Mo\n", statex.ullAvailPageFile / (1024.0 * 1024.0));
}
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    TRACE("Before allocation\n");
    ReportMemory();
 
    const unsigned int taille_bloc = 10* 1024, nb_blocs_max = (3U * 1024U * 1024U * 1024U / taille_bloc);
    int i = 0;
 
    char ** p = NULL;
 
    try
    {
        p = new char *[nb_blocs_max];
 
        memset(p, 0, nb_blocs_max * sizeof(char *));
 
        for(i = 0; i < nb_blocs_max; i++)
        {
            p[i] = new char[taille_bloc];
        }
    }
    catch (CMemoryException* e)
    {
        e->Delete();
 
        TRACE("After the program ran out of memory\n");
        ReportMemory();
 
        for(i = 0; i < nb_blocs_max; i++)
        {
            if(p[i])
            {
                delete [] p[i];
            }
        }
 
        delete [] p;
    }
 
    TRACE("After freeing memory\n");
    ReportMemory();
 
    char * t = NULL;
    try
    {
        const unsigned int taille_max = 30 * 1024 * 1024;
        t = new char[taille_max];
    }
    catch (CMemoryException* e)
    {
        TRACE("Memory allocation failed\n");
        e->Delete();
 
        if(t)
        {
            delete [] t;
        }
    }
 
    return 0;
}
 
Check the virtual available memory after the application free the memory ...
And check the next allocation of 30 Mb just after ...
Any Idea ?
 
Here's the result of the execution
Before allocation
GlobalMemoryStatusEx:
 + TotalVirtual : 2047.9 Mo
 + AvailVirtual : 2016.8 Mo
 + Process mem load : 1.5 %
 + TotalPageFile : 5198.6 Mo
 + AvailPageFile : 3172.9 Mo
 
After the program ran out of memory
GlobalMemoryStatusEx:
 + TotalVirtual : 2047.9 Mo
 + AvailVirtual : 4.5 Mo
 + Process mem load : 99.8 %
 + TotalPageFile : 5198.6 Mo
 + AvailPageFile : 1155.2 Mo
 
After freeing memory
GlobalMemoryStatusEx:
 + TotalVirtual : 2047.9 Mo
 + AvailVirtual : 5.7 Mo
 + Process mem load : 99.7 %
 + TotalPageFile : 5198.6 Mo
 + AvailPageFile : 3170.8 Mo


modified 8-Feb-13 6:58am.

QuestionCross page posting PinmemberTanaji.C30-Jan-13 0:38 
AnswerRe: Cross page posting PinmvpRichard MacCutchan30-Jan-13 2:56 
AnswerRe: Cross page posting PinprotectorPete O'Hanlon30-Jan-13 3:16 
GeneralRe: Cross page posting PinmemberTanaji.C30-Jan-13 17:10 
GeneralRe: Cross page posting PinprotectorPete O'Hanlon30-Jan-13 18:50 
AnswerRe: Cross page posting PinmemberEdo Tzumer2-Feb-13 23:09 
QuestionFiring event when creating instance of a class in C#.net PinmemberMir Hassan Shams Moosavi23-Jan-13 6:00 
AnswerRe: Firing event when creating instance of a class in C#.net PinmemberPaulo Zemek23-Jan-13 6:09 
QuestionAssistant in the work of the Graduation Project Pinmemberalaa_2220-Jan-13 5:14 
Questionosk to notepad Pinmembersrinivasankrishnaa16-Jan-13 4:08 
AnswerRe: osk to notepad PinmvpRichard MacCutchan16-Jan-13 4:17 
Questionshortcut keys Pinmembersrinivasankrishnaa16-Jan-13 4:04 
AnswerRe: shortcut keys PinmemberMike Meinz23-Jan-13 3:28 
GeneralRe: shortcut keys Pinmembersrinivasankrishnaa16-Feb-13 3:20 
GeneralRe: shortcut keys PinmemberMike Meinz16-Feb-13 4:08 
QuestionC++/Cx syntax error PinmemberSuper Lloyd14-Jan-13 2:47 
NewsProblem solved PinmemberSuper Lloyd14-Jan-13 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 24 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid