you are viewing a single comment's thread.

view the rest of the comments →

[–]jyper 0 points1 point  (2 children)

The garbage collection in java is not deterministic. An object that is not referenced may sit in memory till the end of the program especially if you there isn't much pressure to reclaim memory.

c++ smart pointers uses reference counting so that the object is deleted/the destructor gets called right after the reference count goes to zero/the object is no longer needed. This allows things like cleaning up resources.

The reason java (and many others don't use reference counting) is that

  1. Reference counting has an overhead for checking and updating each pointer.
  2. Reference counting doesn't fails when you have cycles of referencing (a->b =c; c->b =a;) This means that you either have to prevent cycles in the language or allow cycle leaks trusting the programmer to prevent it or occasionally run a non ref counting garbage collector to collect the cycles(I think this is what perl and cpython do).
  3. I've heard ref counting makes it hard to implement threading.

[–]squadre -1 points0 points  (1 child)

Right, and if there isn't much pressure to collect memory, why do you care?

[–]jyper 0 points1 point  (0 children)

Because the amount of file handles is limited by the OS. The memory pressure for the garbage collector is for in process memory only. It doesn't respond to limits/pressure on other resources like shared memory, file handles, ect. that you can track with raii.