Program uses a lot of memory and doesnt exit properly

This is a discussion on Program uses a lot of memory and doesnt exit properly within the Windows Programming forums, part of the Platform Specific Boards category; Watching my program in Task Manager it uses a lot of memory. Also when I exit the program (by clicking ...

  1. #1
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    Smile Program uses a lot of memory and doesnt exit properly

    Watching my program in Task Manager it uses a lot of memory. Also when I exit the program (by clicking the X close buttion) the form disappears but it remains running in task manager and the memory use continues to stay high - what could be reasons for this?

    Why isnt it closing and why is it keeping so much memory in use?

    The program uses a lot of arrays and pointers but Ive tried to remove all of these after use, eg if I have a pointer called point,

    Code:
    delete point;
    point = NULL;
    Is this sufficient?

    Are there any other reasons for the problem?

    Thanks for any help

    TJJ

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Hard to say without posting more code

    If you do
    point = new Point[100];

    Be sure to do
    delete [] point;
    and not
    delete point;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    Smile

    Thanks for your reply

    I have been making sure the entire array is deleted as you say but I still have huge memory use.

    At the moment, I have tried to delete pointers whenever Im not using them anymore and I also delete almost all of them at the end of the main routine.

    However, the memory isnt being freed properly. One thing I have just noticed which is worrying is that when the program is running CPU use in very low (about 3%) but as soon as I close the program the CPU usage hit 100%!

    What could cause this - could this give me a clue as to what isnt freeing properly?

    Thanks for any further help

    TJJ

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It would help if you would post some code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    At the point you do
    delete [] array;
    that memory isn't immediately given back to the OS such that task manager will notice a change. It is held internally so that calling new again will be a lot less expensive. Asking the OS for more memory is a pretty expensive thing to do, so programs usually a) take big bites when asking, and b) don't give it back until the program actually finishes.

    Normally this means your program uses some memory and then it levels off as your program hits a peak. It can then call new and delete as much as it wants, and the overall amount used (as seen by task manager) doesn't change.

    If you're seeing continual increases, then you have a problem in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you're using multithreading at all, make sure that the threads are exiting. In a program I once wrote, I accidentally fork( )ed without returning. Worst part was, it was inside a loop. It really made me appreciate Linux's killall command.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> Also when I exit the program (by clicking the X close buttion) the form disappears but it remains running in task manager <<

    Your WM_DESTROY should look like this:

    Code:
    case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
    and your message loop should look something like this:
    Code:
        while(GetMessage(&msg, NULL, 0, 0) > 0)
        { 
                TranslateMessage(&msg); 
                DispatchMessage(&msg); 
        } 
     
        // Return the exit code to the system. 
        return msg.wParam;
    When you post the quit message GetMessage() will return 0 and your program will exit.

    As for the memory problem, make sure that you are looking at the VM Size column and not the Mem Usage column in task manager.

  8. #8
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    Smile

    Thanks for all the replies

    I think Salems reply fits what is happening - the memory usage always hits a peak then stays steady.

    Ive found that once the program hits the peak, if I minimize it, Task Manager reports it as having tiny amounts of memory usage, and if I maximize it, memory goes back to its steady high again - I think this fits with what Salem said?

    Im still curious as to why when I do exit, the program remains in task manager with its high memory and the CPU maxes out? Could this be to do with it taking time and resources to give memory back to the OS?

    As for posting code, Im not sure what to post which would be useful. Below is an example of a function which is using pointers:
    Code:
    void UpdateBVaue()
    {
    	
    	double *PointerToData;
    	PointerToData = new double[AccData[1]->DataCount()];
    
    	double *PointerToNewData;
    	PointerToNewData = AccData[1]->SecEE();
    
    	for(i=0; i<10; i++)
    	{
    PointerToData[i] = PointerToNewData[i]->RetNewData();
    	}
    
    	delete[] PointerToData;
    	PointerToData = NULL;
    
    	delete[] PointerToNewData;
    	PointerToNewData = NULL;
    
    }

    Thanks for any further help

    TJJ

  9. #9
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15
    Just saw anonytmouse's reply - Ill go and have a check with those ideas and see if it helps....

  10. #10
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15
    I had been looking at Memory Usage in Task Manager, but looking at VM Size it is doing the same thing.

    The VM Size goes high with the mem use but stays constant and high if I minimize or close the program. Mem use goes low when I minimize and goes high and constant when I exit.

    As for the code segments you mention Im not sure about them as I am very new to Windows Programming. Ive been using the MFC wizard to make the form. Ive tried searching for the code you mention but it doesnt find anything - could this be the problem?

    Ive noticed that when I exit and the memory goes high and the CPU maxes, and the app stays in task manager, if I wait for about 3-5 minutes eventually the program disappears from task manager, and the CPU goes back to normal - so the program does close but seems to take a long time and lots of resources to do so- can I speed that up?

    Thanks for any further assistance - your help is much appreciated

    TJJ

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Im still curious as to why when I do exit, the program remains in task manager
    This sounds like a bug in your code - most programs disappear from task manager shortly after any visible presence of them disappears from the screen.

    If your program is deleting lots of memory a few bytes at a time, then this could take a while.
    It's not strictly the correct thing to do, but you could just exit the program without calling all those deletes, and the OS will just reclaim the memory anyway (its all virtual).

    Procexp is a much better tool than task manager for rummaging around the innards of processes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    Smile

    Thanks for your help

    Is there a way of making the memory return to Windows quicker? Without any delete statements, it still seems to take a long time to properly close the program - is there a way of just dumping all memory back?

    TJJ

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your program should close(ie. disappear from task manager) immediately(or near enough) when you close the last window. If it is not doing this, then there is something wrong with your code.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    i had a similar problem when i made program with my new entrypoint and

    // Return the exit code to the system.
    return msg.wParam;

    not working properly

    i used ExitProcess()

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21