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. Memory Allocation/Deallocation issues??

Memory Allocation/Deallocation issues??

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancetutorialquestion
8 Posts 4 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.
  • P Offline
    P Offline
    pavanbabut
    wrote on last edited by
    #1

    I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan

    M B M 4 Replies Last reply
    0
    • P pavanbabut

      I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan

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

      pavanbabut wrote:

      Is there something I am doing wrong??

      As long as you are freeing all memory that you allocate, then no :)

      "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

      1 Reply Last reply
      0
      • P pavanbabut

        I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        You can make calls to GetProcessWorkingSetSize and GlobalMemoryStatus to see if the numbers are changing.

        P 1 Reply Last reply
        0
        • B Blake Miller

          You can make calls to GetProcessWorkingSetSize and GlobalMemoryStatus to see if the numbers are changing.

          P Offline
          P Offline
          pavanbabut
          wrote on last edited by
          #4

          Whenever I check it, it is freeing only half of the allocated size. But it frees the whole allocated memory when I deallocate it at the time of terminating the application, not while 'm running the application. -Pavan.

          1 Reply Last reply
          0
          • P pavanbabut

            I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan

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

            How are you checking this? There's no accurate way that I know of. Try this if you'd like:

            MEMORYSTATUS OrigMemoryStat;
            ::GlobalMemoryStatus(&PrevMemoryStat);

            TRACE(_T("********************************\n"));

            for (int i = 0; i < 100; ++i)
            {
            HGLOBAL hGlobal = ::GlobalAlloc(GHND, 2000);
            ::GlobalFree(hGlobal);

            MEMORYSTATUS MemoryStat;
            ::GlobalMemoryStatus(&MemoryStat);
            TRACE(_T("********************************\n"));
            // TRACE(_T("dwTotalPhys %lu Bytes\n"),MemoryStat.dwTotalPhys);
            // TRACE(_T("dwAvailPhys %lu Bytes\n"),MemoryStat.dwAvailPhys);
            // TRACE(_T("dwTotalVirtual %lu Bytes\n"),MemoryStat.dwTotalVirtual);
            // TRACE(_T("dwAvailVirtual %lu Bytes\n"),MemoryStat.dwAvailVirtual);
            // TRACE(_T("dwMemoryLoad %lu%%\n"),MemoryStat.dwMemoryLoad);
            TRACE(_T("** Delta dwAvailPhys %li Bytes\n"), (long)MemoryStat.dwAvailPhys - (long)OrigMemoryStat.dwAvailPhys);
            TRACE(_T("** Delta dwAvailVirtual %li Bytes\n"), (long)MemoryStat.dwAvailVirtual - (long)OrigMemoryStat.dwAvailVirtual);
            }

            TRACE(_T("********************************\n"));

            Trust the OS :~ :suss:

            "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

            P 1 Reply Last reply
            0
            • M Mark Salsbery

              How are you checking this? There's no accurate way that I know of. Try this if you'd like:

              MEMORYSTATUS OrigMemoryStat;
              ::GlobalMemoryStatus(&PrevMemoryStat);

              TRACE(_T("********************************\n"));

              for (int i = 0; i < 100; ++i)
              {
              HGLOBAL hGlobal = ::GlobalAlloc(GHND, 2000);
              ::GlobalFree(hGlobal);

              MEMORYSTATUS MemoryStat;
              ::GlobalMemoryStatus(&MemoryStat);
              TRACE(_T("********************************\n"));
              // TRACE(_T("dwTotalPhys %lu Bytes\n"),MemoryStat.dwTotalPhys);
              // TRACE(_T("dwAvailPhys %lu Bytes\n"),MemoryStat.dwAvailPhys);
              // TRACE(_T("dwTotalVirtual %lu Bytes\n"),MemoryStat.dwTotalVirtual);
              // TRACE(_T("dwAvailVirtual %lu Bytes\n"),MemoryStat.dwAvailVirtual);
              // TRACE(_T("dwMemoryLoad %lu%%\n"),MemoryStat.dwMemoryLoad);
              TRACE(_T("** Delta dwAvailPhys %li Bytes\n"), (long)MemoryStat.dwAvailPhys - (long)OrigMemoryStat.dwAvailPhys);
              TRACE(_T("** Delta dwAvailVirtual %li Bytes\n"), (long)MemoryStat.dwAvailVirtual - (long)OrigMemoryStat.dwAvailVirtual);
              }

              TRACE(_T("********************************\n"));

              Trust the OS :~ :suss:

              "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

              P Offline
              P Offline
              pavanbabut
              wrote on last edited by
              #6

              Ok, I figured out the problem. Once I allocate the memory, I am saving data into the variable by calling another function, in this process I am allocating more memory to another variable to hold a bitmap data, which is not being deallocated later and its piling up. -Pavan.

              1 Reply Last reply
              0
              • P pavanbabut

                I am having some weired situation going on in my VC++ program when I allocate and deallocate memory wihtin the application. Lemme explain the scenario, I am having an applicaiton where I allocate memory to a variable (which are initialized to NULL when the program starts) whenever the user requests and only deallocates it when the user requests the same variable again where I reallocate the memory again. But when I deallocate the memory it is not releasing the whole memory that I have allocated it before. For example- My program starts with total mem usage of 92596 bytes (At this point I haven't allocated any memory to those variables) When I allocate memory to the first variable using either GlobalAlloc or VirtualAlloc it increases by 94752 bytes which equals to ~2000bytes When I deallocate the memory using either GlobalFree or VirtualFree it is deallocating only half of the memory ~1000bytes Now When I reallocate the memory in the same way as above, it is allocating new 2000bytes. So, in this way slowly the memory being used by my program is increasing drastically after a while, as I sometimes allocate about 225 variables of ~2000bytes each. But when I deallocate the memory of the same variables when I am exiting the application it does deallocate ~2000bytes of each variable. Is there something I am doing wrong??:confused: thanks. -Pavan

                M Offline
                M Offline
                Maximilien
                wrote on last edited by
                #7

                have a look at this : http://www.flounder.com/howbig.htm [^]


                Maximilien Lincourt Your Head A Splode - Strong Bad

                P 1 Reply Last reply
                0
                • M Maximilien

                  have a look at this : http://www.flounder.com/howbig.htm [^]


                  Maximilien Lincourt Your Head A Splode - Strong Bad

                  P Offline
                  P Offline
                  pavanbabut
                  wrote on last edited by
                  #8

                  Nice article... thanks..:)

                  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