1

I've heard several mentions that Ada supports garbage collection --- and looking at the language design, it's obviously been designed with that in mind.

I have a non-real-time application for which garbage collection would be really, really useful. However I haven't seen any mention of there being a garbage collector available for my compiler, GNAT. This surprises me; even C supports garbage collection, by simply linking against libgc.

If I simply add libgc to my linker line, will it work or will horrible things happen?

David Given
  • 13,277
  • 9
  • 76
  • 123
  • Incidentally, while it doesn't answer my question, if anyone can point me at a decent (i.e. not too cumbersome) Ada equivalent to C++'s `shared_ptr`, I would gladly use that instead of libgc. – David Given May 19 '14 at 21:58
  • You could always try to run ACATS with libgc, and see if it reports any errors. – Jacob Sparre Andersen May 20 '14 at 07:29
  • 2
    `shared_ptr`: try AdaCore Gems [97](http://www.adacore.com/adaanswers/gems/gem-97-reference-counting-in-ada-part-1/), [99](http://www.adacore.com/adaanswers/gems/gem-99-reference-counting-in-ada-part-2-task-safety/), [100](http://www.adacore.com/adaanswers/gems/gem-100-reference-counting-in-ada-part-3-weak-references/), [107](http://www.adacore.com/adaanswers/gems/gem-107-preventing-deallocation-for-reference-counted-types/), [123](http://www.adacore.com/adaanswers/gems/gem-123-implicit-dereferencing-in-ada-2012/). – Simon Wright May 20 '14 at 08:20
  • Also [GNATColl.Refcount](http://docs.adacore.com/gnatcoll-docs/refcount.html). – Marc C May 20 '14 at 20:46

2 Answers2

1

However I haven't seen any mention of there being a garbage collector available for my compiler, GNAT. This surprises me; even C supports garbage collection, by simply linking against libgc.

You could use GNAT targeting the JVM.

I think that the reason simply linking against libgc won't give you garbage collection [at least to my knowledge] is because GNAT doesn't have any notion of how to use it. However, given that GNAT uses the GCC backend I don't see why it shouldn't "just work" if it does w/ C.

Shark8
  • 4,095
  • 1
  • 17
  • 31
0

I have found a libgc binding as part of the AdaCL library here: http://adacl.sourceforge.net/

However, the libgc documentation also says that libgc can't see pointers stored in blocks allocated via system malloc(). This means that an access which is only stored in an object allocated from the default memory pool won't be considered a root and may be invalidated at any point. This disqualifies the use of standard Ada containers from storing accesses to collectable objects! This might also apply to the secondary stack --- but I don't know what the secondary stack is used for.

However, my investigations do show that garbage collection does naively work:

while true loop
  p := new big_thing_t;
end loop;

...runs forever. So it's probably theoretical possible to do, but right now I don't think it's safe.

David Given
  • 13,277
  • 9
  • 76
  • 123
  • The secondary stack is used for unconstrained objects - objects with parameters unknown at compile-time. For instance a string initialized with a function call, or a matrix m x n. This removes much of the need for garbage collection - and for pointers at all. – Zerte Mar 23 '19 at 20:59