What is the garbage collector in Java? -


I'm new in Java and I'm confused about the garbage collector in Java. What it really does and when it comes into action please describe some of the properties of garbage collector in java.

This is a program that relieves objects that are no longer used by any Java application going. This is a form of automatic memory management .

When a typical Java application is running, it's such as string s and file s, but after a certain time, those items For example, take a look at the following code:

 for  (file: files) {string s = f.getName (); }  

In the above code, strings is being created on every currency of for the loop, that means in each iteration, A small amount of memory is allocated to create string objects.

By going back to the code, we can see that once the run is executed, in the next iteration, the string created in the previous move is no longer being used - This object is now considered "garbage".

In the end, we will start getting 'lot of garbage', and memory will be used for those things which are no longer being used if it continues, eventually the Java virtual machine New objects will be out of space to make them.

This is where the garbage collector moves inside.

Trash collector will search for objects that are no longer being used and rid them, other new objects can use that piece of memory to free the memory.

In Java, memory management is taken care by the garbage collector, but in other languages ​​like C, anyone needs to manage memory themselves using the tasks such as One of those things that makes mistakes easier, which are called - at places where memory is not used when they do not use them.

Automatic memory management schemes such as garbage collections are, therefore, the programmer does not have to worry so much about the issues of memory management, so he can focus more on developing those applications that he has developed Need to do


Comments