Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to print application memory

how to print application memory

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancetutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zecodela
    wrote on last edited by
    #1

    Hi All, i have an MFC application receive event message and based on the message type to new "Order" object. the created object stored in a map and never delete thro' the application cycle. my "Order" class size only < 1k byte. however, the application memory grow as 100k per order inputted. i added the CRT memory leak statement (use _CrtMemDifference, _CrtMemCheckpoint) to see the difference after executed function for each event. the result can show the object size (Order class) i created. however, the 100K grow rate still not able to show. questions: A) i wonder i dont know what reflect in those functions. anyone have idea on this? B) more effective question is how can i print the memory size of my application? so, i can check the memory usage difference for each big function call and drill down the problematic function. like this : printf("b4 handler"); printMemSize(); BigHandler(); printf("after handler"); printMemSize(); any reply will be highly appreciated! thanks! jim

    M 1 Reply Last reply
    0
    • Z zecodela

      Hi All, i have an MFC application receive event message and based on the message type to new "Order" object. the created object stored in a map and never delete thro' the application cycle. my "Order" class size only < 1k byte. however, the application memory grow as 100k per order inputted. i added the CRT memory leak statement (use _CrtMemDifference, _CrtMemCheckpoint) to see the difference after executed function for each event. the result can show the object size (Order class) i created. however, the 100K grow rate still not able to show. questions: A) i wonder i dont know what reflect in those functions. anyone have idea on this? B) more effective question is how can i print the memory size of my application? so, i can check the memory usage difference for each big function call and drill down the problematic function. like this : printf("b4 handler"); printMemSize(); BigHandler(); printf("after handler"); printMemSize(); any reply will be highly appreciated! thanks! jim

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Maybe try something like this:

      // Add this block of code to ONE cpp source file

      #ifdef _DEBUG

      class _MemStateCheck
      {
      public:
      CMemoryState oldMemState, newMemState, diffMemState;

        \_MemStateCheck();
        ~\_MemStateCheck();
      

      };

      _MemStateCheck::_MemStateCheck()
      {
      oldMemState.Checkpoint();
      }

      _MemStateCheck::~_MemStateCheck()
      {
      newMemState.Checkpoint();
      if( diffMemState.Difference( oldMemState, newMemState ) )
      {
      TRACE( "************************\n" );
      TRACE( "Memory leaks Detected\n" );
      TRACE( "************************\n" );
      diffMemState.DumpStatistics();
      diffMemState.DumpAllObjectsSince();
      }
      else
      {
      TRACE( "************************\n" );
      TRACE( "No memory leaks Detected\n" );
      TRACE( "************************\n" );
      }
      }

      _MemStateCheck MemCheckObj;

      #endif //#ifdef _DEBUG

      // Add this to the top of EVERY cpp source file (under the #includes)

      #ifdef _DEBUG
      #define new DEBUG_NEW
      #undef THIS_FILE
      static char THIS_FILE[] = __FILE__;
      #endif

      The code in the first block checks for leaks between the construction and destruction of the static _MemStateCheck MemCheckObj variable. It also shows how you can get memory checkpoints and dump the difference between them to the debugger output window. The code in the second block sets the use of the debug new operator and insures file/linenumber info is available so the leak dump has more useful information. Mark

      This episode brought to you by the number 5

      Z 1 Reply Last reply
      0
      • M Mark Salsbery

        Maybe try something like this:

        // Add this block of code to ONE cpp source file

        #ifdef _DEBUG

        class _MemStateCheck
        {
        public:
        CMemoryState oldMemState, newMemState, diffMemState;

          \_MemStateCheck();
          ~\_MemStateCheck();
        

        };

        _MemStateCheck::_MemStateCheck()
        {
        oldMemState.Checkpoint();
        }

        _MemStateCheck::~_MemStateCheck()
        {
        newMemState.Checkpoint();
        if( diffMemState.Difference( oldMemState, newMemState ) )
        {
        TRACE( "************************\n" );
        TRACE( "Memory leaks Detected\n" );
        TRACE( "************************\n" );
        diffMemState.DumpStatistics();
        diffMemState.DumpAllObjectsSince();
        }
        else
        {
        TRACE( "************************\n" );
        TRACE( "No memory leaks Detected\n" );
        TRACE( "************************\n" );
        }
        }

        _MemStateCheck MemCheckObj;

        #endif //#ifdef _DEBUG

        // Add this to the top of EVERY cpp source file (under the #includes)

        #ifdef _DEBUG
        #define new DEBUG_NEW
        #undef THIS_FILE
        static char THIS_FILE[] = __FILE__;
        #endif

        The code in the first block checks for leaks between the construction and destruction of the static _MemStateCheck MemCheckObj variable. It also shows how you can get memory checkpoints and dump the difference between them to the debugger output window. The code in the second block sets the use of the debug new operator and insures file/linenumber info is available so the leak dump has more useful information. Mark

        This episode brought to you by the number 5

        Z Offline
        Z Offline
        zecodela
        wrote on last edited by
        #3

        Thanks! Mark. This is what i did but i used the _CRTMemXXXX function from crtdbg.h. i guess your CMemoryState is from MFC. I have i question. If there is no memory leak, but i want to print the app's allocated memory size (sth like the "memory" column in task manager). Any function can serve this purpose? thanks! jim

        M 1 Reply Last reply
        0
        • Z zecodela

          Thanks! Mark. This is what i did but i used the _CRTMemXXXX function from crtdbg.h. i guess your CMemoryState is from MFC. I have i question. If there is no memory leak, but i want to print the app's allocated memory size (sth like the "memory" column in task manager). Any function can serve this purpose? thanks! jim

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Maybe this: Collecting Memory Usage Information For a Process[^] Mark

          This episode brought to you by the number 5

          Z 1 Reply Last reply
          0
          • M Mark Salsbery

            Maybe this: Collecting Memory Usage Information For a Process[^] Mark

            This episode brought to you by the number 5

            Z Offline
            Z Offline
            zecodela
            wrote on last edited by
            #5

            Mark, Thank you for your quick response! let me take a look! Jim

            E 1 Reply Last reply
            0
            • Z zecodela

              Mark, Thank you for your quick response! let me take a look! Jim

              E Offline
              E Offline
              epankaj
              wrote on last edited by
              #6

              How can i print application Memory in some file?

              Pankaj

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups