10

How does JVM own memory OS and how JVM cleans its objects in heaps?

 2 years ago
source link: https://www.codesd.com/item/how-does-jvm-own-memory-os-and-how-jvm-cleans-its-objects-in-heaps.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How does JVM own memory OS and how JVM cleans its objects in heaps?

advertisements

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?


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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK