Question

When our application finishes its work, what does happen with memory allocated by the system (for our JVM). Does GC delete all objects? How does JVM release memory and how does Mac OS clean up JVM process with its memory.

When I close my application using System.exit(1), what does my JVM do with GC and memory?

Was it helpful?

Solution

Whether the garbage collector runs or not is JVM-specific behavior, but in most cases it will not (and I'll explain why when I talk about the OS). If you want to execute some operation on exit (such as a call to System.gc() to suggest that the garbage collector should run) then you can use the Runtime.addShutdownHook API. Note that this is a suggestion, and may not actually run the GC in all cases.

At the end of System.exit(), Java will make an exit() syscall to Mac OS. When this happens, all threads in the Java process will be halted by the operating system and the pages of memory it was using will be deallocated by the OS. This is different than the deallocation that Java would run on an object, which frees up a (usually very small) piece of memory for the Java process to reuse, but Java still has total control over the memory and the OS cannot give it to another process. When the OS deallocates a page of memory, it means that there is now a physical region of memory that can be allocated by another process or the OS itself (for i.e. filesystem caching). Since the OS only allocates and deallocates memory by multiples of a page of memory (usually around 4k bytes) it can deallocate memory somewhat more quickly than Java (which may have many objects to process inside of a single page).

The reason it doesn't make sense for Java to deallocate all the memory it's using before exiting (by running the GC on exit) is that once the program exits, it doesn't matter what was in those pages because nobody cares what data was last held in the Java process. The OS will ensure that any pages it allocates to a new process are scrubbed clean of the old data beforehand.

OTHER TIPS

When you call System.exit(1) Garbage Collector might or might not run, there is no guarantee here. How Mac OS (or any other OS) reclaims memory for a particular process is totally dependent on the OS that you use. The guarantee here is that once a process finishes, the memory will be reclaimed.

I assume that the simplest mode to do it, is knowing the start and finish address of the memory used by the process, and once finished it is just reclaimed. But I'm not sure.

You can read a bit here about some info about Mac OS Memory management: Mac OS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top