4 ms·
Why do you need to clean up if your program is about to exit?
by chrisseaton 7y ago
Why do you need to clean up if your program is about to exit?
- mike_hock 7y agoHence my question.
- chrisseaton 7y agoI still don't understand your question, sorry: > can't the regular GCs just not clean up on program exit Why do you need to clean up your memory - at all, GC or otherwise - on program exit? The process and all its resources will be gone.
- peterwwillis 7y agoThe question was rhetorical; you and OP are saying the same thing.
- pests 7y agoI think the concern is those resources might be external and not cleaning up correctly leaves them in an inconsistent state. Not saying this is best practice but I've seen it done.
- chrisseaton 7y agoFinalisers are not guaranteed to be called by GC in theory, and in practice they run asynchronously even if they are going to be called, so aren't likely to be called if you GC and then exit. So how does calling GC help with anything?
- pests 7y agoI agree with you and how it's not reliable. I just remember the Rust community going through this same kerfuffle with their Drop trait not being guaranteed not too long ago.
- steveklabnik 7y agoThat was four and a half years ago; time flies!
- mike_hock 7y agoBut regular GCs DO run at the end of the program, so my question was, why don't they just skip the final cleanup.
- chrisseaton 7y ago> But regular GCs DO run at the end of the program Which GCs do that? I'm not aware of any.
- balfirevic 7y agoThe article (apparently falsely) claims that default JVM GC does that: "Short-running programs, like all programs, invoke the garbage collector at the end of their run."
- MaxBarraclough 7y agoUnless I'm missing something, the only reason you'd run a GC cycle on termination is to invoke finalizers. If the platform doesn't feature finalizers, or if it is known that they've not been used, there'd be no point at all.
- zbentley 7y agoMemory might need to be cleaned up if the program was being run embedded in something else (it's not unheard of to embed JVMs inside e.g. C++ applications, and it's very common in scripting languages to do this). Additionally, global destructors, while not guaranteed, can be very helpful if you let them run rather than just exiting and letting the system clean up file descriptors: for example, a clean disconnect from a database is often faster overall (on the database side, e.g. freeing up a connection slot) than a dirty "client hasn't phoned in for awhile/received unexpected FIN" disconnect via hard-exit.
- chrisseaton 7y ago> Memory might need to be cleaned up if the program was being run embedded in something else Just unmap the heap pages. Don't run the GC! > global destructors, while not guaranteed, can be very helpful if you let them run If you want them to run on exit then you want Runtime.runFinalizersOnExit, not the GC. Finalizers are non-deterministic, asynchronous, and would take an indefinite number of GC cycles to run them for all objects.