Hacker News new | ask | show | jobs
by toast0 1481 days ago
Won't Java or C# run the destructor eventually when they GC the equivalent of fstream?

Yes, that means they hang around a lot longer, and that's sometimes problematic, but that's the GC way.

1 comments

To be technically correct, it’s called a finalizer in that case to distinguish it from deterministic destructors.

For resources like memory, where it’s merely a question of performance, I would agree. But some resources have correctness implications. Files, for example, you often want to close deterministically. It’s simply a terrible experience if a user can’t save because a file handle from a previous operation still lingers in a GC queue somewhere.

That’s also the reason why language features like try-with-resources were added in the first place.