Using garbage collection in C++

Discussion in 'C++' started by Pedro Miguel Carvalho, Sep 26, 2004.

  1. Greetings.

    I'm creating a project that as a intricate relation between object kind of
    like a set where each object in the set can be connect to a subset of object
    of the set and objects not in the set can connect to objects in the set.
    Every object inherits a interface to allow for a mark and sweep garbage
    collector (GC) and my implementation works correctly and efficiently but
    only in a single thread.

    How can I garbage collect in a multi-thread program?

    With my garbage collector all threads have to stop what they are doing but
    in a coherent state before the garbage collector can do it's job. I could
    signal all thread that a garbage collection is needed and wait for all the
    treads to be ready then execute the garbage collection but it would mean
    that some thread would have to wait (possibly a long time) before all thread
    are ready and the garbage collector can start.


    I have also tried the Hans-Boehm conservative GC (HBGC) in single-thread
    (both the interface for my GC and my GC are off) and it worked correctly. It
    takes 5-8% more CPU time, very acceptable, but it uses 79-96% more memory
    and that is not acceptable (measured max and mix CPU and memory used in 100
    test runs). The test runs uses less CPU time and memory than a full run. In
    fact in a full run the HBGC would use virtual memory in my 512 MB
    development system and that as you can imagine is a big no-no.

    Is this excess memory use normal or am I doing something wrong?

    Comments are very much appreciated.

    Thanks,
    Pedro Carvalho
     
    Pedro Miguel Carvalho, Sep 26, 2004
    #1
    1. Advertising

  2. Pedro Miguel Carvalho wrote:
    > How can I garbage collect in a multi-thread program?


    This is off-topic for C++. Try looking for help in an algorithms
    newsgroup, or do a search on the subject of thread synchronization.

    Hint: interlock the access to your "ready to garbage collect" member
    variable. There's no reason why all threads have to stop when you
    garbage collect.
     
    Dave Rahardja, Sep 26, 2004
    #2
    1. Advertising

  3. Pedro Miguel Carvalho wrote:
    > I have also tried the Hans-Boehm conservative GC (HBGC) in single-thread
    > (both the interface for my GC and my GC are off) and it worked correctly. It
    > takes 5-8% more CPU time, very acceptable, but it uses 79-96% more memory
    > and that is not acceptable (measured max and mix CPU and memory used in 100
    > test runs). The test runs uses less CPU time and memory than a full run. In
    > fact in a full run the HBGC would use virtual memory in my 512 MB
    > development system and that as you can imagine is a big no-no.


    Hans-Boehm should work fine. If you want to avoid the extra storage it
    requires between collection cycles, try adding a reference-counting
    mechanism to some of your acyclic structures (for example, by using
    smart pointers to refer to them). Then there will be less garbage
    building up between cycles. You may also be able to make it collect more
    often, or when usage reaches a certain level.
    --
    Derrick Coetzee
    I grant this newsgroup posting into the public domain. I disclaim all
    express or implied warranty and all liability. I am not a professional.
     
    Derrick Coetzee, Sep 27, 2004
    #3
  4. "Derrick Coetzee" <> wrote in message
    news:cj7ifv$9ej$...
    | Pedro Miguel Carvalho wrote:
    | > I have also tried the Hans-Boehm conservative GC (HBGC) in single-thread
    | > (both the interface for my GC and my GC are off) and it worked
    correctly. It
    | > takes 5-8% more CPU time, very acceptable, but it uses 79-96% more
    memory
    | > and that is not acceptable (measured max and mix CPU and memory used in
    100
    | > test runs). The test runs uses less CPU time and memory than a full run.
    In
    | > fact in a full run the HBGC would use virtual memory in my 512 MB
    | > development system and that as you can imagine is a big no-no.
    |
    | Hans-Boehm should work fine. If you want to avoid the extra storage it
    | requires between collection cycles, try adding a reference-counting
    | mechanism to some of your acyclic structures (for example, by using
    | smart pointers to refer to them). Then there will be less garbage
    | building up between cycles. You may also be able to make it collect more
    | often, or when usage reaches a certain level.
    | --
    | Derrick Coetzee

    One of my implementations uses reference-counting smart-pointers for some
    objects but for the data set I use a mark-sweep GC. I'm trying to use a GC
    because of the extensive cycles that form on my data set so basic
    reference-counting will not be a solution. If I make the Hans-Boehm
    collector work more then it's use of the CPU also increases.

    Thanks,
    Pedro Carvalho
     
    Pedro Miguel Carvalho, Sep 27, 2004
    #4
  5. "Dave Rahardja" <> wrote in message
    news:pXF5d.40074$-kc.rr.com...
    | Pedro Miguel Carvalho wrote:
    | > How can I garbage collect in a multi-thread program?
    |
    | This is off-topic for C++. Try looking for help in an algorithms
    | newsgroup, or do a search on the subject of thread synchronization.

    Sorry about that. I looked for an appropriate newsgroup. I found various
    *.graphics.algorithms, not appropriate, and various *.comp.algorithms, none
    in english. I will try in comp.programming.threads, not exactly what I need,
    another off-topic :).

    | Hint: interlock the access to your "ready to garbage collect" member
    | variable. There's no reason why all threads have to stop when you
    | garbage collect.

    To garbage collect the data set I have to be certain that I see a consistent
    state, other GC use write/read barriers for this.

    Thanks,
    Pedro Carvalho
     
    Pedro Miguel Carvalho, Sep 27, 2004
    #5
    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. Kieran McPeake
    Replies:
    0
    Views:
    4,996
    Kieran McPeake
    May 30, 2006
  2. Replies:
    1
    Views:
    441
    mrstephengross
    Jul 25, 2005
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    983
    Øyvind Isaksen
    May 18, 2007
  4. SteveB
    Replies:
    2
    Views:
    280
    SteveB
    Oct 20, 2008
  5. Eric Mahurin
    Replies:
    25
    Views:
    376
    Trans
    Oct 11, 2005
Loading...

Share This Page