Does the JVM free all resources that the programmer does not explicitly close when exiting the program

I always heard that resources in java should be closed after use or these resources will be exhausted. Is this really a question for small programs that use very few resources (like 1 or 2 file readers / buffered readers and all)? Doesn't the JVM keep track of resources used by the program? And will he not release all these resources after the program? If not, why does the JVM block these resources even after the program exits?

+4
source share
4 answers

These resources are really closed during a regular JVM exit. However, you can’t always find out how you call your method, and you could call it 2,000 times outwardly another programmer, and these resources will begin to add up.

In addition, some non-mainstream operating systems may run into a problem: if the JVM was supposed to stop abnormally (via System.halt or crash), then the resources may remain open (due to non-working cleaning code), potentially unusable until it reboots or will be released manually. Even on major systems, sockets can remain open for several minutes.

+2
source

When the process exits, the JVM exits. Then what happens is that the OS will release the resources previously used by the JVM. This includes things like memory, file descriptors, etc.

0
source

This is not a JVM, but an OS that frees up resources allocated to the JVM process after it exits.

0
source

Small programs can live with resource leaks, since the OS can handle this after the process is complete. But this is not considered portable.

The big problem is that the OS in which the JVM runs does not know how to free resources allocated on the remote computer, or if there are even dedicated resources that need to be freed.

0
source

Source: https://habr.com/ru/post/1500885/


All Articles