Using garbage collection in C++

  • Thread starter Pedro Miguel Carvalho
  • Start date
P

Pedro Miguel Carvalho

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
 
D

Dave Rahardja

Pedro said:
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.
 
D

Derrick Coetzee

Pedro said:
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.
 
P

Pedro Miguel Carvalho

| 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
 
P

Pedro Miguel Carvalho

| 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top