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. Very slow function free()

Very slow function free()

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
10 Posts 5 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.
  • B Offline
    B Offline
    bschorre
    wrote on last edited by
    #1

    Hi all, I'm trying a little bit with the malloc() and free() functions. Unfourtunatly, if alloc 256MB the malloc-function needs 10 Seconds to get the memory. That's not nice but what I'm really suprised at is that free-function needs 278 Seconds to drop the memory. ---------------------------------------------------- bufferSize = 256*1024*1024; if((buffer = (char *) malloc(bufferSize)) == NULL) { cout << "Could not allocate enough memory." << endl; exit -1; } p_BufferRoot = buffer; // ... put stuff in memory ... free(buffer); ---------------------------------------------------- Can anyone explain what happens to me free-function?! THX triathlet394

    D H 2 Replies Last reply
    0
    • B bschorre

      Hi all, I'm trying a little bit with the malloc() and free() functions. Unfourtunatly, if alloc 256MB the malloc-function needs 10 Seconds to get the memory. That's not nice but what I'm really suprised at is that free-function needs 278 Seconds to drop the memory. ---------------------------------------------------- bufferSize = 256*1024*1024; if((buffer = (char *) malloc(bufferSize)) == NULL) { cout << "Could not allocate enough memory." << endl; exit -1; } p_BufferRoot = buffer; // ... put stuff in memory ... free(buffer); ---------------------------------------------------- Can anyone explain what happens to me free-function?! THX triathlet394

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Allocating memory can be an expensive operation, especially if memory is fragmented. On low-memory machines, asking for 256MB of contigious RAM might involve a lot of paging. Freeing memory instructs the memory manager to access pages previously put on disk. That might be where the latency comes from.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      B 1 Reply Last reply
      0
      • D David Crow

        Allocating memory can be an expensive operation, especially if memory is fragmented. On low-memory machines, asking for 256MB of contigious RAM might involve a lot of paging. Freeing memory instructs the memory manager to access pages previously put on disk. That might be where the latency comes from.


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        B Offline
        B Offline
        bschorre
        wrote on last edited by
        #3

        How can I resolve this problem a little bit more comfortable? triathlet394

        D B M 3 Replies Last reply
        0
        • B bschorre

          How can I resolve this problem a little bit more comfortable? triathlet394

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          I do not know, specifically. Have you tried the new and delete operators instead?


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

          1 Reply Last reply
          0
          • B bschorre

            How can I resolve this problem a little bit more comfortable? triathlet394

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

            Do you really need 256 MB? Can you break up your data or structure into sub-elements? You might also try VirtualAlloc instead of using malloc and free. VirtualAlloc has some options that malloc does not give you.

            B 1 Reply Last reply
            0
            • B bschorre

              How can I resolve this problem a little bit more comfortable? triathlet394

              M Offline
              M Offline
              Mad__
              wrote on last edited by
              #6

              Try use GlobalAlloc with GMEM_MOVEABLE flag, this may be work fast.

              B 1 Reply Last reply
              0
              • M Mad__

                Try use GlobalAlloc with GMEM_MOVEABLE flag, this may be work fast.

                B Offline
                B Offline
                bschorre
                wrote on last edited by
                #7

                Hi, I've read that the GlobalAlloc-Function-Group come from the Win 3.x-Framework. What's the difference between GlobalAlloc and VirtualAlloc? triathlet394

                M 1 Reply Last reply
                0
                • B Blake Miller

                  Do you really need 256 MB? Can you break up your data or structure into sub-elements? You might also try VirtualAlloc instead of using malloc and free. VirtualAlloc has some options that malloc does not give you.

                  B Offline
                  B Offline
                  bschorre
                  wrote on last edited by
                  #8

                  Thanks Blake, It works really fine. And my programm isn't look big if your looking in the Task-Manager for the memory-usage. triathlet394

                  1 Reply Last reply
                  0
                  • B bschorre

                    Hi, I've read that the GlobalAlloc-Function-Group come from the Win 3.x-Framework. What's the difference between GlobalAlloc and VirtualAlloc? triathlet394

                    M Offline
                    M Offline
                    Mad__
                    wrote on last edited by
                    #9

                    Yes, you right, GlobalAlloc come from Win3.x, but hi work with whole address space, and if you have 256M free in phisical memory, hi get you memory block in phisic memory ;). And if you use VirtualAlloc function, you mast manage usage of pages. About speed : GlobalAlloc work some slower then VirtualAlloc, but i didn't think, that is critical (for 256M on Win2k diference less 1s).

                    1 Reply Last reply
                    0
                    • B bschorre

                      Hi all, I'm trying a little bit with the malloc() and free() functions. Unfourtunatly, if alloc 256MB the malloc-function needs 10 Seconds to get the memory. That's not nice but what I'm really suprised at is that free-function needs 278 Seconds to drop the memory. ---------------------------------------------------- bufferSize = 256*1024*1024; if((buffer = (char *) malloc(bufferSize)) == NULL) { cout << "Could not allocate enough memory." << endl; exit -1; } p_BufferRoot = buffer; // ... put stuff in memory ... free(buffer); ---------------------------------------------------- Can anyone explain what happens to me free-function?! THX triathlet394

                      H Offline
                      H Offline
                      Henry miller
                      wrote on last edited by
                      #10

                      For what complier/C library? malloc/free is not provided by any OS I'm aware of. Instead your C library (which might come with the compiler, might be included) provides it. In some Unix systems (and I suspect Windows as well), the library asks the OS for more memory than you requested, and then holds that in a buffer. If you malloc more, it will give you memroy from that same buffer if there is room. When you free the memory it marks it unused, but doesn't return it to the OS, instead it will hold onto it in case you need more memory latter. On many systems the OS will only allow you to allocate 4Kb chunks (this varies depending on both the OS and the design of the hardware). The library is designed so that you can malloc a few bytes, without getting a full 4kb. Also, in many cases the library will write something to most of that memory when it is allocated. The OS however may just mark your application as allowed to use that much memory, but not actually give it to you, until you use it. In effect it is optimized for small accesses, and you are sending it a large acccess which it isn't designed for. If you don't need portability you might be able to speed things up by using OS specific functions to get the memory from the OS yourself. Note that none of this will help if you don't have enough physical memory. If you want to use 256M of memory you should have at least 400M of physical memory so the OS doesn't have page everything else out.

                      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