repeat malloc & memory fragmentation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spasmous

    repeat malloc & memory fragmentation

    Yo,

    I'm running a subroutine that mallocs a 10 or so Mb, then frees it on
    exit. Then when it's called again it mallocs/frees again, and so on
    for maybe 100 calls.

    Sometimes the code fails after running a long time - is it possible
    that malloc needs a contiguous block of memory but my frequent mallocs
    have fragged it all up and so the later mallocs fail. Or... am I just
    a bad programmer ;)

    Thanks for your input.
  • lilburne

    #2
    Re: repeat malloc & memory fragmentation

    spasmous wrote:[color=blue]
    >
    > Sometimes the code fails after running a long time - is it possible
    > that malloc needs a contiguous block of memory but my frequent mallocs
    > have fragged it all up and so the later mallocs fail.
    >[/color]

    Yes malloc will require a contigous block of memory.

    Comment

    • David Harmon

      #3
      Re: repeat malloc & memory fragmentation

      On Sun, 01 Feb 2004 19:41:06 +0000 in comp.lang.c++, lilburne
      <lilburne@godzi lla.net> was alleged to have written:[color=blue]
      >spasmous wrote:[color=green]
      >>
      >> Sometimes the code fails after running a long time - is it possible
      >> that malloc needs a contiguous block of memory but my frequent mallocs
      >> have fragged it all up and so the later mallocs fail.
      >>[/color]
      >
      >Yes malloc will require a contigous block of memory.[/color]

      But it will more often be found that the program has a memory leak
      (forgetting to free something that should be) and is actually running
      out of available.

      Comment

      • lilburne

        #4
        Re: repeat malloc &amp; memory fragmentation

        David Harmon wrote:
        [color=blue]
        > On Sun, 01 Feb 2004 19:41:06 +0000 in comp.lang.c++, lilburne
        > <lilburne@godzi lla.net> was alleged to have written:
        >[color=green]
        >>spasmous wrote:
        >>[color=darkred]
        >>>Sometimes the code fails after running a long time - is it possible
        >>>that malloc needs a contiguous block of memory but my frequent mallocs
        >>>have fragged it all up and so the later mallocs fail.
        >>>[/color]
        >>
        >>Yes malloc will require a contigous block of memory.[/color]
        >
        >
        > But it will more often be found that the program has a memory leak
        > (forgetting to free something that should be) and is actually running
        > out of available.
        >[/color]

        Maybe, but then he doesn't say that the program has started
        paging prior to running out of memory.

        Comment

        • David Harmon

          #5
          Re: repeat malloc &amp; memory fragmentation

          On Sun, 01 Feb 2004 20:35:38 +0000 in comp.lang.c++, lilburne
          <lilburne@godzi lla.net> was alleged to have written:[color=blue][color=green]
          >> But it will more often be found that the program has a memory leak
          >> (forgetting to free something that should be) and is actually running
          >> out of available.[/color]
          >
          >Maybe, but then he doesn't say that the program has started
          >paging prior to running out of memory.[/color]

          Maybe, but he doesn't say much. Is there any more reason to think the
          program would resort to paging to satisfy memory requests when the heap
          was full than it would when the heap is fragmented?

          Comment

          • Paul

            #6
            Re: repeat malloc &amp; memory fragmentation

            spasmous wrote:
            [color=blue]
            > Yo,
            >
            > I'm running a subroutine that mallocs a 10 or so Mb, then frees it on
            > exit. Then when it's called again it mallocs/frees again, and so on
            > for maybe 100 calls.
            >
            > Sometimes the code fails after running a long time - is it possible
            > that malloc needs a contiguous block of memory but my frequent mallocs
            > have fragged it all up and so the later mallocs fail. Or... am I just
            > a bad programmer ;)
            >
            > Thanks for your input.[/color]

            How about looking at what you're doing between the calls of malloc and
            free? Maybe something in there is causing the problem. To convince
            yourself, why not write a very simple program that just allocates and
            frees 10 Meg of memory in a loop?

            #include <cstdlib>

            void allocate_stuff( )
            {
            char *p = (char *)malloc( 10000000 );
            free(p);
            }

            int main()
            {
            char *p;
            for ( int i = 0; i < 100; ++i )
            allocate_stuff( );
            }

            This code does what you describe. Does it cause problems as you've
            described them, or does it run smoothly?

            Paul

            Comment

            Working...