Do languages with garbage collectors necessarily have to be object oriented? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

Do languages with garbage collectors necessarily have to be object oriented?

Are there any non-OO languages that have GCs?

21st Apr 2019, 12:31 AM
Sonic
Sonic - avatar
8 Answers
+ 16
Any language which allows you to allocate and reserve memory resource in the heap, will require garbage collection, either a built-in garbage collector handles it, or you do manual memory management. In other words, if a language allows dynamic memory allocation, GC will be involved. Non-OO languages which allows dynamic memory allocation exist, and hence GC and memory management of the likes is not specific to OO languages. Doesn't sound like my cup of tea, but this existed. https://www.linuxjournal.com/article/6679 also, https://stackoverflow.com/questions/9952602/does-haskell-require-a-garbage-collector
21st Apr 2019, 2:00 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Hatsy Rei thanks for the articles. I particularly like the quote which says "C programmers think memory management is too important to be left to the computer. LISP programmers think memory management is too important to be left to the user.” Jared Bird yes it seems that there is no direct relationship between OOP and automatic GC apart from similar timing of when they became popular/widespread. It also seems to me that one challenge of adding an automatic GC to C is it's weakly typedness. So maybe there is more a link between automatic GCs and strongly typedness than between automatic GCs and OO-ness?
21st Apr 2019, 4:19 AM
Sonic
Sonic - avatar
+ 7
hmm Strong types, GC: Java, Haskell Strong types, no GC: C++, Rust Weak types, GC: Javascript, Prolog Weak types, no GC: C, asm Seems to me that there are just many more languages with a gc than not. I think the main differentiator is the intent of the language, most of the non-gc'd languages seem to be used for systems programming.
21st Apr 2019, 6:39 AM
Schindlabua
Schindlabua - avatar
+ 7
Schindlabua seems like strong typedness is not at all correlated with auto GC then. I was just going by the statement in the above article about the DBW collector algorithm which is only one implementation of a GC BTW, where it has to make assumptions due to not being able to completely determine which variables are actual pointers to dynamic memory and which ones are not. I am not tempted to assume any link between GC'd languages and systems programming either as someone can always come up with a counter example. But maybe it's a necessary condition of a language used for systems programming to not be GC'd. So I think you're right.
21st Apr 2019, 7:58 AM
Sonic
Sonic - avatar
+ 5
Hatsy Rei That's not my my cup of tea either. Apparently someone thought it was a good idea to add a GC to gcc (written in c++), this has been suggested as the biggest reason gcc takes twice as long to compile code as clang. I think the reason GC and OOP, seem to match up is timing. Both became popular around the same time. Notice that some languages allow you to use OO designs or not, but treat GC in the same way.
21st Apr 2019, 3:17 AM
Jared Bird
Jared Bird - avatar
+ 5
Jared Bird Well that depends on your definition of weak. There is no right answer. However, javascript can do this: a = 4; a = "hello"; and this: if([1] == "1") Type coercion breaks the language so much, even the inbuilt `isNaN` function is broken and essentially useless for checking for a NaN type value. (Don't get me started on array sorting!) And to typecheck a value by hand you have to do some weird combination of `typeof` and `instanceof` and `.constructor` to make sure you cover everything. But it doesn't cover everything because if you get a value from another <iframe> two of these don't help. Anyway I could rant about js types for years, I hope you can feel my pain :D Those are not features I expect of a strong type system. C's and JS's type systems are not that similar but both are pretty weak.
22nd Apr 2019, 8:58 AM
Schindlabua
Schindlabua - avatar
+ 2
Hmm... Schindlabua I wouldn't say Javascript and C have the same or similar type system. I would say C is weak (or loose) static typed; while Javascript is dynamic typed. Although the reason for C not having auto GC, is more likely based on design and implementation.
21st Apr 2019, 11:04 PM
Jared Bird
Jared Bird - avatar
+ 2
Schindlabua Your right that there is no 1 right answer. And yes I SO feel your pain. 😩 I have not needed to write any Javascript for a few years now, and I don't miss any of those issues. 😓 I don't have any issues like that when coding in C. Kinda makes the manual memory management look easy.
23rd Apr 2019, 1:45 AM
Jared Bird
Jared Bird - avatar