I am new to Java and confused about the garbage collector in Java. What does it actually do and when does it comes into action. Please describe some of the properties of the garbage collector in Java.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Automatic garbage collection is a process where the JVM gets rid of or keeps certain data points in memory to ultimately free up space for the running program. Memory is first sent to heap memory, that is where the garbage collector (GC) does its work, then is decided to be terminated or kept. Java assumes that the programmer cannot always be trusted, so it terminates items it thinks it doesn't need.
garbage collector implies that objects that are no longer needed by the program are "garbage" and can be thrown away.
Many people think garbage collection collects and discards dead objects.
In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage.
When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system. To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm.
Check this for more detail information: http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx
The garbage collector allows your computer to simulate a computer with infinite memory. The rest is just mechanism.
It does this by detecting when chunks of memory are no longer accessible from your code, and returning those chunks to the free store.
EDIT: Yes, the link is for C#, but C# and Java are identical in this regard.