More about the garbage collector
Hi! In the last lesson, we first became acquainted with Java's built-in garbage collector and got a rough idea of how it works.
It works in the background while your program is running, collecting unnecessary objects that will be deleted later. Thus,...
hashnode.com
Nice article. I will admit, that I was a bit skeptical that Java uses Tracing garbage collection instead of traditional reference counting. So I made a quick test to see if java will not get Out-Of-Memory exception when bunch of cyclic objects will be constantly generated in a loop :
public class gctest { private gctest self; public gctest() { this.self = this; } public static void main(String []args) { while (true) { new gctest(); } } }Indeed Java program has not crashed and GC perfectly cleaned circular references.
I only need to mention that traditional Reference counting GC model has also advanced and now is able to clean circular object references too, because there exists special algorithms in RC model which searches for a cycles in references.
The same GC test in PHP (which uses reference counting GC model) :
class GCtest { private $me; public function __construct() { $this->me = $this; } } while (true) { new GCtest(); }Script also has not crashed and traditional RC GC model cleaned objects with circular references. The only difference between Java and PHP in this case which as was able to notice - is that Java program has consumed ~ 100% of memory, but PHP version has not altered memory usage. Probably this means that Java GC collecting cycles starts at far lower rate than in PHP. Thus creation of objects fills heap at a rate at which Java GC collector is not able to catch-up.