freeing memory in shortest time

Discussion in 'C Programming' started by vivek, Mar 5, 2008.

  1. vivek

    vivek Guest

    Hello,

    I have a pointer to a main structure which again consists of
    structures, enums, char, int, float and again complex structures.

    When i free all the contents of the main structure, it takes me a lot
    of time (since i have to loop determining the data type and freeing
    it). Is there any idea to free all the contents of the structure in
    shortest possible time.
     
    vivek, Mar 5, 2008
    #1
    1. Advertising

  2. vivek

    vivek Guest

    Or probably should i switch to static memory instead??? In that case
    what care should be taken.

    Pl advice
     
    vivek, Mar 5, 2008
    #2
    1. Advertising

  3. vivek

    santosh Guest

    vivek wrote:

    > Hello,
    >
    > I have a pointer to a main structure which again consists of
    > structures, enums, char, int, float and again complex structures.
    >
    > When i free all the contents of the main structure, it takes me a lot
    > of time (since i have to loop determining the data type and freeing
    > it). Is there any idea to free all the contents of the structure in
    > shortest possible time.


    You might try making the structure static. Or you might try having a
    separate cache of all the pointers to dynamic memory for each structure
    element. This will consume additional memory, but will eliminate the
    need to traverse the structure to free all it's components.

    In anycase, I doubt that traversing the structure is going to be your
    most serious bottleneck, particularly if you do it all at once.
     
    santosh, Mar 5, 2008
    #3
  4. vivek

    vivek Guest

    Oh, it is for an embedded system, where that freeing the structure
    takes about 0.4 ms, which is a lot of time for an embedded
    system...Thats why!!!
     
    vivek, Mar 5, 2008
    #4
  5. vivek said:

    > Hello,
    >
    > I have a pointer to a main structure which again consists of
    > structures, enums, char, int, float and again complex structures.
    >
    > When i free all the contents of the main structure, it takes me a lot
    > of time (since i have to loop determining the data type and freeing
    > it). Is there any idea to free all the contents of the structure in
    > shortest possible time.


    Well, exit(0) will do it. But I'm curious to know why you think it's taking
    a lot of time. Computers are pretty fast nowadays. Is this really the
    bottleneck for your program? What did profiling tell you?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 5, 2008
    #5
  6. vivek

    vivek Guest

    "you might try having a
    > separate cache of all the pointers to dynamic memory for each structure
    > element. This will consume additional memory, but will eliminate the
    > need to traverse the structure to free all it's components.
    >
    > In anycase, I doubt that traversing the structure is going to be your
    > most serious bottleneck, particularly if you do it all at once"


    Can you explain in detail about the caching, please? I could not
    follow that part.
     
    vivek, Mar 5, 2008
    #6
  7. vivek

    santosh Guest

    vivek wrote:

    > Oh, it is for an embedded system, where that freeing the structure
    > takes about 0.4 ms, which is a lot of time for an embedded
    > system...Thats why!!!


    Okay. Some of your options are:

    1. Don't free anything. Obviously this might not be feasible.
    2. Use static or automatic objects. Static objects will persist
    throughout the program lifetime while auto objects will be destroyed
    once their scope is exited.
    3. Use something like alloca. See the recent threads on this. Objects
    allocated with alloca persist throughout the function and need no
    explicit call to free.
    4. Modify your data structures so that dynamic memory is minimised
    or "clumped" together, thus minimising the number calls to free.

    There may be other strategies too, though we can't tell you which one
    might be suitable since the choice depends on various factors of your
    program and it's host machine and what's expected of them.

    Generally speaking, if you want realtime performance then malloc/free is
    pretty much ruled out, but I would suggest that you actually verify
    that they *are* the problem and that the time consumed *is*
    unacceptable before considering other strategies.
     
    santosh, Mar 5, 2008
    #7
  8. vivek

    vivek Guest

    Of course everything else is ok with the program
    functionalities...except for the time...If time is ok...thats
    it..project over...

    and thank u so much santhosh
     
    vivek, Mar 5, 2008
    #8
  9. On 5 Mar, 08:47, vivek <> wrote:

    > I have a pointer to a main structure which again consists of
    > structures, enums, char, int, float and again complex structures.


    are these nested structures or pointed to?

    /* headers ommitted */

    struct Main1_s
    {
    struct Nested_s nested;
    };

    struct Main2_s
    {
    struct Pointed_s *pointed;
    };

    void func (void)
    {
    struct Main1_s *main1_s;
    struct Main2_s *main2_s;

    /* should check for malloc() failures */
    main1_s = malloc(sizeof *main1_s);
    main2_s = malloc(sizeof *main2_s);
    main2_s->pointed = malloc(sizeof (*main2_s->pointed));

    /* do stuff */

    /* cleanup */
    delete (main1_s);
    /* nested freed automatically */

    delete (main2_s->pointed);
    delete (main2_s);
    }



    > When i free all the contents of the main structure, it takes me a lot
    > of time (since i have to loop determining the data type and freeing
    > it).


    what sort of time? Do you mean the program takes a long time?
    How do you know, have you timed it? Or is it taking a long
    time to write the program? Why do you need a loop to determine the
    data type. We need to see some code!

    > Is there any idea to free all the contents of the structure in
    > shortest possible time.


    not sure what you mean. Could you use automatic storage?

    void func (void)
    {
    struct Main1_s main1_s;

    /* do stuff */
    }

    no mallocs therefore no deletes



    and then vivek wrote:
    > Or probably should i switch to static memory instead??? In that case
    > what care should be taken.


    I'm not sure. What lifetime does your structure need? Statics can
    occupy storage unnecessarily (they go away only when the program
    ends).
    You may make them accessible from too much of the program.
    You might have problems that there is only one copy of the struct
    when logically you need many.

    I need to know more about what you are doing.
    Can you post a simpified example?


    --
    Nick Keighley

    The Dinosaurs have come and gone, we Theriodonts remain
     
    Nick Keighley, Mar 5, 2008
    #9
  10. vivek

    vivek Guest

    The structures are nested.

    I have measured the time taken for this specific code section to run.

    The lifetime of the structures would be like this.

    Receive some communication data, use these structures to decode data -
    allot memory...after decoding, process it ..then free memory..

    then prepare a reply, encode the date(create these structures-allot
    memory), send the data and free memory.
     
    vivek, Mar 5, 2008
    #10
  11. vivek

    CBFalconer Guest

    santosh wrote:
    > vivek wrote:
    >
    >> I have a pointer to a main structure which again consists of
    >> structures, enums, char, int, float and again complex structures.
    >>
    >> When i free all the contents of the main structure, it takes me
    >> a lot of time (since i have to loop determining the data type
    >> and freeing it). Is there any idea to free all the contents of
    >> the structure in shortest possible time.

    >
    > You might try making the structure static. Or you might try
    > having a separate cache of all the pointers to dynamic memory
    > for each structure element. This will consume additional memory,
    > but will eliminate the need to traverse the structure to free
    > all it's components.


    Something is seriously wrong. The type has nothing to do with
    freeing. You simply free the object. The compiler knows what the
    type, and thus the size, of that object is.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Mar 5, 2008
    #11
  12. vivek

    Bartc Guest

    "vivek" <> wrote in message
    news:...
    > Hello,
    >
    > I have a pointer to a main structure which again consists of
    > structures, enums, char, int, float and again complex structures.


    None of these require freeing individually. Only if there are pointers to
    other things (perhaps the complex structures you mentioned).

    > When i free all the contents of the main structure, it takes me a lot
    > of time (since i have to loop determining the data type and freeing
    > it).


    Why do have to determine the data type at runtime? Do you have elements
    which sometimes are pointers and sometimes something else? Or only some
    pointers need freeing?

    >Is there any idea to free all the contents of the structure in
    > shortest possible time.


    Perhaps post the struct definition here so we can see the problem areas.
    But, if the struct will always be the same size and is reused frequently,
    perhaps just keep it allocated -- unless you are very short of memory. But
    even there might be tricks to share the memory without
    allocating/deallocating.

    --
    Bart
     
    Bartc, Mar 5, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh.Rapaka

    freeing memory

    Rajesh.Rapaka, Apr 20, 2005, in forum: Java
    Replies:
    17
    Views:
    2,847
    Eric Sosman
    Apr 21, 2005
  2. Harald Kirsch

    freeing memory

    Harald Kirsch, Apr 22, 2005, in forum: Java
    Replies:
    0
    Views:
    438
    Harald Kirsch
    Apr 22, 2005
  3. Hassan Iqbal
    Replies:
    3
    Views:
    496
    Al Bowers
    Sep 25, 2003
  4. freeing memory....partially

    , Oct 4, 2003, in forum: C Programming
    Replies:
    12
    Views:
    566
  5. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    608
    Jean-Claude Arbaut
    Jun 15, 2005
Loading...

Share This Page