| What is garbage collection? | | | | value will decremented. Thus reference count is small |
| It deals with identifying and freeing the memory of | | | | but constant work happens during the entire life time |
| java application that are not used for a long time. | | | | of your program. The garbage collector move |
| There are two type garbage collection techniques, | | | | through the entire list of the object. When GC find |
| namely explicit and implicit methods. | | | | an object with reference count zero or null, it |
| Implicit Garbage collection and Explicit Garbage | | | | releases the storage. The main problem with the |
| collection | | | | Reference count is when the object has circular |
| Java run time system can invoke the garbage | | | | reference then the GC cant find an object with zero |
| collector automatically so that the user never need | | | | value. Finding the self reference object need an extra |
| to bother about the memory space when creating an | | | | effort. This is why reference GC is not being used in |
| object. This is implicit Garbage Collection. | | | | JVM(Java Virtual Machine) |
| Explicit Garbage Collection means the user is doing | | | | Copy garbage collector |
| the collection intentionally. This is achieved by using | | | | >If you need faster GC it is not possible with the |
| System. gc(); | | | | reference counting. The other GC is based on the |
| Steps in a garbage collection | | | | idea that any non-dead object can access easily |
| 1. garbage collector searches for objects that are | | | | whether they are in stack or static storage. This |
| referenced in code. | | | | chain must go through several object. Thus you can |
| 2. garbage collector finds the unreferenced code. | | | | find all the live object. For each handle you find, you |
| 3. garbage collector frees unreferenced objects and | | | | must trace in to the object that the handle points |
| add their memory space to heap. | | | | and then trace all the handles of that object they |
| Methods in System | | | | points. This will continue after moving through the |
| 1) collect() | | | | entire web. Each object that you passes through |
| Request to the garbage collector to reclaim the | | | | must be alive. The problem with self referential |
| memory to heap. By using this method it not sure | | | | groups can be avoided by this. In this collector type |
| that any inaccessible memory is reclaimedpublic static | | | | JVM use adaptive garbage collection method. What it |
| void Collect() | | | | does is locating the object according to the variant |
| 2)keepAlive() | | | | currently being used. Each live object that found is |
| This method is used to ensure the existence of an | | | | copied from the one heap to another discarding all |
| object. Using this method we can add a value | | | | the garbage. In new heap objects are packed end to |
| reference counter so it is not reclaimed by the | | | | end. |
| garbage collector, we have to pass the object name | | | | Problem with this technique is you need to keep |
| as the parameterpublic static void KeepAlive(Object | | | | twice the memory that you actually need. The |
| obj); | | | | second issue is in stable program, where the garbage |
| Different Garbage Collector | | | | will be very small. But still the copy collector copy all |
| Reference counting | | | | the memory from one to another. |
| One of the simple methods is reference counting. | | | | Mark and Sweep |
| This type of garbage collection is very slow. In this | | | | Mark and Sweep uses same technology as the Copy |
| type of GC each object will have a reference | | | | garbage collector but the main difference is that it will |
| counter that will hold the value of how many times it | | | | not move the live object. In this kind of GC when it |
| has been referred. When a reference to an object is | | | | finds a live object the object is marked by setting |
| made by attaching a handle to it then the counter | | | | the flag. Only after the marking process is finished |
| value will be incremented by one every time the | | | | the sweep occurs. In sweep all dead |
| handle goes out from the object or set to null, that | | | | objects(unmarked) are released and no copying |
| means the reference is no longer exists the counter | | | | happens. |