Q: Allocating memory correctly

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

    Q: Allocating memory correctly

    Hi,

    Whenever allocating memory using operator new or operator new[] I feel
    like I should only use it very sparingly, because otherwise memory is wasted
    (by additional overhead needed to manage all those allocations) which also
    loses some performance. I am specifically talking about many little
    allocations (approx. 16-512 bytes). Is my feeling about this justified?

    thanks!
    --
    jb

    (replace y with x if you want to reply by e-mail)


  • Moonlit

    #2
    Re: Allocating memory correctly

    Hi,


    "Jakob Bieling" <netsurf@gmy.ne t> wrote in message
    news:bqv2fm$jvf $01$1@news.t-online.com...[color=blue]
    > Hi,
    >
    > Whenever allocating memory using operator new or operator new[] I feel
    > like I should only use it very sparingly, because otherwise memory is[/color]
    wasted[color=blue]
    > (by additional overhead needed to manage all those allocations) which also
    > loses some performance. I am specifically talking about many little
    > allocations (approx. 16-512 bytes). Is my feeling about this justified?
    >[/color]
    Typically new has to search for a free block that is large enough. Automatic
    variables are usually faster (because it is just some space on the stack).
    Try to use variables on the stack as much as possible. For variables that
    take up a lot of space or that you just have to create dynamically use
    dynamic allocation.

    If it slows down your program you can always overload the new operator and
    make a smarter (and likely more wasteful considering memory) implementation.

    Regards, Ron AF Greve.
    [color=blue]
    > thanks!
    > --
    > jb
    >
    > (replace y with x if you want to reply by e-mail)
    >
    >[/color]


    Comment

    • Jakob Bieling

      #3
      Re: Allocating memory correctly

      "Moonlit" <alt.spam@jupit er.universe> wrote in message
      news:3fd31475$0 $210$e4fe514c@n ews.xs4all.nl.. .
      [color=blue][color=green]
      > > Whenever allocating memory using operator new or operator new[] I[/color][/color]
      feel[color=blue][color=green]
      > > like I should only use it very sparingly, because otherwise memory is[/color]
      > wasted[color=green]
      > > (by additional overhead needed to manage all those allocations) which[/color][/color]
      also[color=blue][color=green]
      > > loses some performance. I am specifically talking about many little
      > > allocations (approx. 16-512 bytes). Is my feeling about this justified?
      > >[/color]
      > Typically new has to search for a free block that is large enough.[/color]
      Automatic[color=blue]
      > variables are usually faster (because it is just some space on the stack).
      > Try to use variables on the stack as much as possible. For variables that
      > take up a lot of space or that you just have to create dynamically use
      > dynamic allocation.[/color]

      Hi,

      in my case I cannot use automatic variables, since the allocations are
      either an array of unknown size (but at least 1 element) or the variable
      needs to live longer than automatic variables would. But since a lot of
      those allocations might be done, I am asking myself if it is necessary to
      write a memory manager myself or if using many vector's helps (not sure
      about its allocator, but I am guessing/hoping that it uses the same memory
      pool for the same kind of vectors?) or if everything is fine the way I have
      it.

      thanks
      --
      jb

      (replace y with x if you want to reply by e-mail)


      Comment

      • Moonlit

        #4
        Re: Allocating memory correctly

        Hi,

        "Jakob Bieling" <netsurf@gmy.ne t> wrote in message
        news:bqv5ih$6cq $02$1@news.t-online.com...[color=blue]
        > "Moonlit" <alt.spam@jupit er.universe> wrote in message
        > news:3fd31475$0 $210$e4fe514c@n ews.xs4all.nl.. .
        >[color=green][color=darkred]
        > > > Whenever allocating memory using operator new or operator new[] I[/color][/color]
        > feel[color=green][color=darkred]
        > > > like I should only use it very sparingly, because otherwise memory is[/color]
        > > wasted[color=darkred]
        > > > (by additional overhead needed to manage all those allocations) which[/color][/color]
        > also[color=green][color=darkred]
        > > > loses some performance. I am specifically talking about many little
        > > > allocations (approx. 16-512 bytes). Is my feeling about this[/color][/color][/color]
        justified?[color=blue][color=green][color=darkred]
        > > >[/color]
        > > Typically new has to search for a free block that is large enough.[/color]
        > Automatic[color=green]
        > > variables are usually faster (because it is just some space on the[/color][/color]
        stack).[color=blue][color=green]
        > > Try to use variables on the stack as much as possible. For variables[/color][/color]
        that[color=blue][color=green]
        > > take up a lot of space or that you just have to create dynamically use
        > > dynamic allocation.[/color]
        >
        > Hi,
        >
        > in my case I cannot use automatic variables, since the allocations are
        > either an array of unknown size (but at least 1 element) or the variable
        > needs to live longer than automatic variables would. But since a lot of
        > those allocations might be done, I am asking myself if it is necessary to
        > write a memory manager myself or if using many vector's helps (not sure[/color]

        I wouldn't start with that.
        [color=blue]
        > about its allocator, but I am guessing/hoping that it uses the same memory
        > pool for the same kind of vectors?) or if everything is fine the way I[/color]
        have[color=blue]
        > it.[/color]

        If I where you I would use vector<> for arrays.

        Then later on when your app actually does run too slow and an analysis of
        your code shows it is due to new/delete you might invest some time in
        overloading new/delete operator (do not release the memory for the class but
        insert it into a linked list then when a new is done return the top of the
        list).

        Regards, Ron AF Greve.
        [color=blue]
        >
        > thanks
        > --
        > jb
        >
        > (replace y with x if you want to reply by e-mail)
        >
        >[/color]


        Comment

        • Pete Becker

          #5
          Re: Allocating memory correctly

          Jakob Bieling wrote:[color=blue]
          >
          > in my case I cannot use automatic variables, since the allocations are
          > either an array of unknown size (but at least 1 element) or the variable
          > needs to live longer than automatic variables would.[/color]

          Those are good reasons to use the free store.
          [color=blue]
          > But since a lot of
          > those allocations might be done, I am asking myself if it is necessary to
          > write a memory manager myself[/color]

          If you can write a memory manager that's better than the one that comes
          with your compiler, by all means do it. <g> One advantage that you might
          have is that you know the details of your application's memory usage. If
          so, you might be able to do a better job for your application than the
          general-purpose memory manager that comes with your compiler. But be
          warned: memory managers are hard to write, and even harder to debug,
          because the symptoms of a bug usually show up far from the point where
          the actual error occurs.
          [color=blue]
          > or if using many vector's helps (not sure
          > about its allocator, but I am guessing/hoping that it uses the same memory
          > pool for the same kind of vectors?)[/color]

          Maybe. But don't underestimate the capabilities of the memory manager
          that comes with the compiler. They're often quite good.
          [color=blue]
          > or if everything is fine the way I have it.
          >[/color]

          If your application doesn't meet its requirements and you've determined
          that the cause is the memory manager then you have to replace the memory
          manager. If you application meets its requirements then everything is
          fine the way you have it.

          --

          Pete Becker
          Dinkumware, Ltd. (http://www.dinkumware.com)

          Comment

          • Jakob Bieling

            #6
            Re: Allocating memory correctly

            "Pete Becker" <petebecker@acm .org> wrote in message
            news:3FD331AF.8 99A2D03@acm.org ...[color=blue]
            > Jakob Bieling wrote:[color=green]
            > >
            > > in my case I cannot use automatic variables, since the allocations[/color][/color]
            are[color=blue][color=green]
            > > either an array of unknown size (but at least 1 element) or the variable
            > > needs to live longer than automatic variables would.[/color]
            >
            > Those are good reasons to use the free store.
            >[color=green]
            > > But since a lot of
            > > those allocations might be done, I am asking myself if it is necessary[/color][/color]
            to[color=blue][color=green]
            > > write a memory manager myself[/color]
            >
            > If you can write a memory manager that's better than the one that comes
            > with your compiler, by all means do it. <g> One advantage that you might
            > have is that you know the details of your application's memory usage. If
            > so, you might be able to do a better job for your application than the
            > general-purpose memory manager that comes with your compiler. But be
            > warned: memory managers are hard to write, and even harder to debug,
            > because the symptoms of a bug usually show up far from the point where
            > the actual error occurs.
            >[color=green]
            > > or if using many vector's helps (not sure
            > > about its allocator, but I am guessing/hoping that it uses the same[/color][/color]
            memory[color=blue][color=green]
            > > pool for the same kind of vectors?)[/color]
            >
            > Maybe. But don't underestimate the capabilities of the memory manager
            > that comes with the compiler. They're often quite good.
            >[color=green]
            > > or if everything is fine the way I have it.
            > >[/color]
            >
            > If your application doesn't meet its requirements and you've determined
            > that the cause is the memory manager then you have to replace the memory
            > manager. If you application meets its requirements then everything is
            > fine the way you have it.[/color]


            Thanks guys, I'll just use the default thingies and see how well it
            performs. :)
            --
            jb

            (replace y with x if you want to reply by e-mail)


            Comment

            • Dan W.

              #7
              Re: Q: Allocating memory correctly

              On Sun, 7 Dec 2003 12:21:31 +0100, "Jakob Bieling" <netsurf@gmy.ne t>
              wrote:
              [color=blue]
              >Hi,
              >
              > Whenever allocating memory using operator new or operator new[] I feel
              >like I should only use it very sparingly, because otherwise memory is wasted
              >(by additional overhead needed to manage all those allocations) which also
              >loses some performance. I am specifically talking about many little
              >allocations (approx. 16-512 bytes). Is my feeling about this justified?[/color]

              Yes it is. That's why the STL provides the means to substitute custom
              allocators, and often package their own allocators. Typically, they
              allocate memory in big chunks, arrays of uninitialized objects that
              can be used for more quick and efficient allocation for small objects.
              The standard allocator is notably slow performing in multithreading
              and multiprocessing situations. STLport, I hear, have a very good
              allocator.

              Comment

              • Pete Becker

                #8
                Re: Q: Allocating memory correctly

                "Dan W." wrote:[color=blue]
                >
                > STLport, I hear, have a very good
                > allocator.[/color]

                "Very good" depends on what your requirements are. The STLport allocator
                is fast, but it doesn't release memory for use by the rest of the
                application.

                --

                Pete Becker
                Dinkumware, Ltd. (http://www.dinkumware.com)

                Comment

                • EventHelix.com

                  #9
                  Re: Allocating memory correctly

                  In general, with C++, you should avoid dynamic memory allocations.
                  Dynamic memory allocation should be considered only after you have
                  determined that a local variable or a fixed size allocation will not
                  do the job.

                  Also, it might be better to use STL than direct dynamic allocations
                  in your code. STL will take care of allocating and freeing memory as
                  needed.

                  Sandeep
                  --
                  Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

                  EventStudio 2.0 - System Architecture Design CASE Tool

                  Comment

                  • Gianni Mariani

                    #10
                    Re: Q: Allocating memory correctly

                    Pete Becker wrote:[color=blue]
                    > "Dan W." wrote:
                    >[color=green]
                    >>STLport, I hear, have a very good
                    >>allocator.[/color]
                    >
                    >
                    > "Very good" depends on what your requirements are. The STLport allocator
                    > is fast, but it doesn't release memory for use by the rest of the
                    > application.
                    >[/color]

                    darn - those nasty trade-offs ... :-)

                    I suppose it keeps us software engineers in jobs - oh wait a sec, some
                    engineers in jobs.



                    Comment

                    • Pete Becker

                      #11
                      Re: Q: Allocating memory correctly

                      Gianni Mariani wrote:[color=blue]
                      >
                      > I suppose it keeps us software engineers in jobs - oh wait a sec, some
                      > engineers in jobs.[/color]

                      :-(

                      --

                      Pete Becker
                      Dinkumware, Ltd. (http://www.dinkumware.com)

                      Comment

                      Working...