简体   繁体   中英

Do java objects get removed when a java application closes?

I've been learning about Java and how it uses garbage collection vs manual deallocation of objects. I couldn't find an answer to whether java objects get removed when a java application closes or not? What exactly happens in the JVM when, say, a small console application with an object

public class Hello {
    public String name = "Y_Y"; 
}

exists in memory and the console application is closed?

Thanks, Y_Y

When an application closes, the jvm stops running and all of its memory is returned to the host.

For all practical purposes, the heap and all object allocated there stop to exist.

If you're concerned about security, any process with raised privileges would be able to scan that memory and read whatever's left around. It would have to do so before the memory gets allocated to another process. But that could also happen while the original program/jvm is running.

You can't know for sure. The behavior is not specified or guaranteed. But you should not care too much about that. What you should care is that the memory is reclaimed.

If security is your issue, well it shouldn't be. Security cases should be treated when encountered. Rewriting the entire memory with 0 or garbage would make exit really slow.

What happens is the memory occupied by the string is freed on exit. If the object implements a finalize() method, it may be called.

Also, you can invoke the Garbage collector manually using System.gc();

The following is an extract from the Sun specifications.

The specification for the Java platform makes very few promises about how garbage collection actually works. Here is what the Java Virtual Machine Specification (JVMS) has to say about memory management.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements.1 While it can seem confusing, the fact that the garbage collection model is not rigidly defined is actually important and useful-a rigidly defined garbage collection model might be impossible to implement on all platforms. Similarly, it might preclude useful optimizations and hurt the performance of the platform in the long term.

Although there is no one place that contains a full definition of required garbage collector behavior, much of the GC model is implicitly specified through a number of sections in the Java Language Specification and JVMS. While there are no guarantees about the exact process followed, all compliant virtual machines share the basic object lifecycle described in this chapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM