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.
The garbage collector is a program which runs on the Java Virtual Machine which gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.
When a typical Java application is running, it is creating new objects, such as String
s and File
s, but after a certain time, those objects are not used anymore. For example, take a look at the following code:
for (File f : files) {
String s = f.getName();
}
In the above code, the String s
is being created on each iteration of the for
loop. This means that in every iteration, a little bit of memory is being allocated to make a String
object.
Going back to the code, we can see that once a single iteration is executed, in the next iteration, the String
object that was created in the previous iteration is not being used anymore -- that object is now considered "garbage".
Eventually, we'll start getting a lot of garbage, and memory will be used for objects which aren't being used anymore. If this keeps going on, eventually the Java Virtual Machine will run out of space to make new objects.
That's where the garbage collector steps in.
The garbage collector will look for objects which aren't being used anymore, and gets rid of them, freeing up the memory so other new objects can use that piece of memory.
In Java, memory management is taken care of by the garbage collector, but in other languages such as C, one needs to perform memory management on their own using functions such as malloc
and free
. Memory management is one of those things which are easy to make mistakes, which can lead to what are called memory leaks -- places where memory is not reclaimed when they are not in use anymore.
Automatic memory management schemes like garbage collection makes it so the programmer does not have to worry so much about memory management issues, so he or she can focus more on developing the applications they need to develop.
It frees memory allocated to objects that are not being used by the program any more - hence the name "garbage". For example:
public static Object otherMethod(Object obj) {
return new Object();
}
public static void main(String[] args) {
Object myObj = new Object();
myObj = otherMethod(myObj);
// ... more code ...
}
I know this is extremely contrived, but here after you call otherMethod()
the original Object
created is made unreachable - and that's "garbage" that gets garbage collected.
In Java the GC runs automatically, but you can also call it explicitly with System.gc()
and try to force a major garbage collection. As Pascal Thivent points out, you really shouldn't have to do this and it might do more harm than good (see this question).
For more, see the wikipedia entry on Garbage collection and Tuning Garbage Collection (from Oracle)
An object becomes eligible for Garbage collection or GC if it's not reachable from any live threads or by any static references.
In other words, you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as the reference so if object A has a reference to object B and object B has a reference to Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection.
Heap Generations for Garbage Collection -
Java objects are created in Heap
and Heap
is divided into three parts or generations for the sake of garbage collection in Java, these are called as Young(New) generation, Tenured(Old) Generation and Perm Area of the heap.
Perm space of Java Heap is where JVM stores Metadata about classes and methods, String pool and Class level details.
Refer here for more : Garbage Collection
You can't force JVM to run Garbage Collection though you can make a request using System.gc()
or Runtime.gc()
method.
In java.lang.System
public static void gc() {
Runtime.getRuntime().gc();
}
In java.lang.Runtime
public native void gc(); // note native method
Mark and Sweep Algorithm -
This is one of the most popular algorithms used by Garbage collection. Any garbage collection algorithm must perform 2 basic operations. One, it should be able to detect all the unreachable objects and secondly, it must reclaim the heap space used by the garbage objects and make the space available again to the program.
The above operations are performed by Mark and Sweep Algorithm in two phases:
- Mark phase
- Sweep phase
read here for more details - Mark and Sweep Algorithm
garbage collector implies that objects that are no longer needed by the program are "garbage" and can be thrown away.
Garbage Collector is part of JRE that makes sure that object that are not referenced will be freed from memory.
It usually runs when you app runs out of memory.
AFAIK it holds a graph that represents the links between the objects and isolated objects can be freed.
To save performance the current objects grouped into generations, each time GC scans an object and finds that it is still referenced its generation count incremented by 1 (to some max maximum value, 3 or 4 i think) , and the new generation are scanned first (the shortest the object in memory the more probably it is no longer needed) so not all objects being scanned every time GC run.
read this for more information.
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.
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
To put it in the most simple terms that even a non-programmer can understand, when a program processes data it creates intermediate data and storage space (variables, arrays, certain object metadata etc.) for that data.
When these objects are accessed across functions or over a certain size, they are allocated from a central heap. Then when they are no long needed, they need to be cleaned up.
There are some very good articles online about how this works, so I'll just cover the very basic definition.
The GC is basically the function that does this cleanup. To do this is clears table entries that aren't referenced by any active objects, effectively deleting the objects, than copies and compacts the memory. It's a little more complicated than this, but you get the idea.
The big problem is some parts this process often requires the entire Java VM to require to be stopped temporarily to take place, as well as this entire process being very processor and memory bandwidth intensive. The various options as of GCs and tuning options for each one are designed to balance these various issues with the whole GC process.
Garbage Collection in Java (and other languages/platforms as well) is a way for the java run-time environment (JRE) to reuse memory from java objects that are no longer needed. Simplistically, when the JRE initially starts up it asks the Operating System (O/S) for a certain amount of memory. As the JRE runs your application(s) it uses that memory. When your application is done using that memory, the JRE's "Garbage Collector" comes along and reclaims that memory for use by different parts of your existing application(s). The JRE's "Garbage Collector" is a background task that is always running and tries to pick times when the system is idle to go on its garbage runs.
A real world analogy would be the garbage men that come to your house and pick up your recyclable garbage... eventually, its reused in other ways by yourself and/or other people.
Garbage collector can be viewed as a reference count manager. if an object is created and its reference is stored in a variable, its reference count is increased by one. during the course of execution if that variable is assigned with NULL. reference count for that object is decremented. so the current reference count for the object is 0. Now when Garbage collector is executed, It checks for the objects with reference count 0. and frees the resources allocated to it.
Garbage collector invocation is controlled by garbage collection policies.
You can get some data here. http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html
Garbage collector is a component of jvm.
It is used to collect garbage when ever cpu gets free.
Here garbage means unused objects it runs in Background of main program
to monitor the status of the main program.
Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
In a programming language like C, allocating and deallocating memory is a manual process. In Java, process of deallocating memory is handled automatically by the garbage collector. Please check the link for a better understanding. http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
Garbage collection refers to the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program. The heap is a memory which is referred to as the free store, represents a large pool of unused memory allocated to your Java application.
The basic principles of garbage collection are to find data objects in a program that cannot be accessed in the future, and to reclaim the resources used by those objects. https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29
Advantages
1) Saves from bugs, which occur when a piece of memory is freed while there are still pointers to it, and one of those pointers is dereferenced. https://en.wikipedia.org/wiki/Dangling_pointer
2) Double free bugs, which occur when the program tries to free a region of memory that has already been freed, and perhaps already been allocated again.
3) Prevents from certain kinds of memory leaks, in which a program fails to free memory occupied by objects that have become unreachable, which can lead to memory exhaustion.
Disadvantages
1) Consuming additional resources, performance impacts, possible stalls in program execution, and incompatibility with manual resource management. Garbage collection consumes computing resources in deciding which memory to free, even though the programmer may have already known this information.
2) The moment when the garbage is actually collected can be unpredictable, resulting in stalls (pauses to shift/free memory) scattered throughout a session. Unpredictable stalls can be unacceptable in real-time environments, in transaction processing, or in interactive programs.
Oracle tutorial http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
Garbage collection is the process identifying which objects are in use and which are not, and deleting the unused objects.
In a programming languages like C, C++, allocating and freeing memory is a manual process.
int * array = new int[size];
processArray(array); //do some work.
delete array; //Free memory
The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.
Step 2a. Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.
To improve performance, we want to delete unreferenced objects and also compact the remaining referenced objects. We want to keep referenced objects together, so it will be faster to allocate new memory.
As stated earlier, having to mark and compact all the objects in a JVM is inefficient. As more and more objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection time.
Continue reading this tutorial, and you will know how GC takes this challenge.
In short, there are three regions of the heap, YoungGeneration for short life objects, OldGeneration for long period objects, and PermanentGeneration for objects that live during the application life, for example, classes, libraries.
As objects are dynamically allocated by the new operator, you can ask how these objects are destroyed and how busy memory is freed. In other languages such as C++, you need to free manually allocated objects dynamically by the delete operator. Java has a different approach; it automatically handles deallocation. The technique is known as Garbage Collection.
It works like this: when there are no references to an object, it is assumed that this object is no longer needed and you can retrieve the memory occupied by the object. It is not necessary to explicitly destroy objects as in C++. Garbage collection occurs sporadically during program execution; It does not simply happen because there are one or more objects that are no longer used. In addition, several Java runtime implementations have different approaches to garbage collection, but most programmers do not have to worry about this when writing programs.
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.
精彩评论