answersLogoWhite

0


Best Answer

It returns memory to the memory pool by destroying objects that no longer have a reference to them.

Memory management is a crucial element in many types of applications. Consider a program that reads in large amounts of data, say from somewhere else on a network, and then writes that data into a database on a hard drive. A typical design would be to read the data into some sort of collection in memory, perform some operations on the data, and then write the data into the database. After the data is written into the database, the collection that stored the data temporarily must be emptied of old data or deleted and recreated before processing the next batch. This operation might be performed thousands of times, and in languages like C or C++ that do not offer automatic garbage collection, a small flaw in the logic that manually empties or deletes the collection data structures can allow small amounts of memory to be improperly reclaimed or lost forever. These small losses are called memory leaks, and over many thousands of iterations they can make enough memory inaccessible that programs will eventually crash. Creating code that performs manual memory management cleanly and thoroughly is a complex task.

Java's garbage collector provides an automatic solution to memory management. In most cases it frees you from having to add any memory management logic to your application. The downside to automatic garbage collection is that you can't completely control when it runs and when it doesn't.

Overview of Java's Garbage Collector

Garbage collection is the phrase used to describe automatic memory management in Java. Whenever a software program executes (in any programming language for that matter), it uses memory in several different ways. We're not going to get into Computer Science 101 here, but it's typical for memory to be used to create a stack, a heap, in Java's case constant pools, and method areas. The heap is that part of memory where Java objects live, and it's the one and only part of memory that is in any way involved in the garbage collection process.

So, all of garbage collection revolves around making sure that the heap has as much free space as possible. For the purpose of the exam, what this boils down to is deleting any objects that are no longer reachable by the Java program running. When the garbage collector runs, its purpose is to find and delete objects that cannot be reached. If you think of a Java program as being in a constant cycle of creating the objects it needs (which occupy space on the heap), and then discarding them when they're no longer needed, creating new objects, discarding them, and so on, the missing piece of the puzzle is the garbage collector. When it runs, it looks for those discarded objects and deletes them from memory so that the cycle of using memory and releasing it can continue.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

garbage collection is automated by the JVM(Java virtual machine).

Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Garbage collection is the process of freeing up unused memory. All classes and methods use the JVMs memory but in most cases they do not get cleared after they get used. The JVM does periodic checks to look for such unused memory objects and deletes them to provide for more space for the active components. The Garbage collection process is automatic and the programmer cannot control as to how and when it works.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

The Garbage Collector program in Java runs automatically every once in a while. We need not provide an implementation of the GC ourselves.

However, we can invoke the GC by calling System.gc() or Runtime.gc(). This will invoke the Garbage Collector but does not guarantee that the garbage collector would run. It will run only when the JVM feels appropriate.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Garbage collection is the phrase used to describe automatic memory management in Java. Whenever a software program executes (in any programming language for that matter), it uses memory in several different ways. We're not going to get into Computer Science 101 here, but it's typical for memory to be used to create a stack, a heap, in Java's case constant pools, and method areas. The heap is that part of memory where Java objects live, and it's the one and only part of memory that is in any way involved in the garbage collection process.

So, all of garbage collection revolves around making sure that the heap has as much free space as possible. For the purpose of the exam, what this boils down to is deleting any objects that are no longer reachable by the Java program running. When the garbage collector runs, its purpose is to find and delete objects that cannot be reached. If you think of a Java program as being in a constant cycle of creating the objects it needs (which occupy space on the heap), and then discarding them when they're no longer needed, creating new objects, discarding them, and so on, the missing piece of the puzzle is the garbage collector. When it runs, it looks for those discarded objects and deletes them from memory so that the cycle of using memory and releasing it can continue.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

We need not implement garbage collection. GC is a default operation that is taken care of, by the JVM and happens by itself at regular intervals of time. As a programmer we do not have any control on how the GC works.

The one thing we can do is, try to invoke the garbage collector using the runtime.gc(); call. This does not guarantee an invocation to run the garbage collector but has a probability of running it. The JVM alone decides when the garbage collection needs to happen.

Note that the ability to either force or influence the execution of a Garbage Collection run is entirely dependent on the specific JVM being used. Certain "realtime" VMs can allow a forced GC to occur immediately, while for other VMs, this is only a "suggestion" that it might be OK to run GC now.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Garbage collection refers to eliminating objects that are no longer accessible, and thus free up the space they use. In Java, for example, once a variable that refers to an object no longer exists (when the method in which it was declared ends execution), the object is usually no longer accessible. However, for reasons of efficiency, the object is not immediately destroyed; rather, at some moment (when the program isn't doing anything, or when memory is required for something), the JVM invokes the garbage collector, which checks for unused objects and eliminates them.

Garbage collection refers to eliminating objects that are no longer accessible, and thus free up the space they use. In Java, for example, once a variable that refers to an object no longer exists (when the method in which it was declared ends execution), the object is usually no longer accessible. However, for reasons of efficiency, the object is not immediately destroyed; rather, at some moment (when the program isn't doing anything, or when memory is required for something), the JVM invokes the garbage collector, which checks for unused objects and eliminates them.

Garbage collection refers to eliminating objects that are no longer accessible, and thus free up the space they use. In Java, for example, once a variable that refers to an object no longer exists (when the method in which it was declared ends execution), the object is usually no longer accessible. However, for reasons of efficiency, the object is not immediately destroyed; rather, at some moment (when the program isn't doing anything, or when memory is required for something), the JVM invokes the garbage collector, which checks for unused objects and eliminates them.

Garbage collection refers to eliminating objects that are no longer accessible, and thus free up the space they use. In Java, for example, once a variable that refers to an object no longer exists (when the method in which it was declared ends execution), the object is usually no longer accessible. However, for reasons of efficiency, the object is not immediately destroyed; rather, at some moment (when the program isn't doing anything, or when memory is required for something), the JVM invokes the garbage collector, which checks for unused objects and eliminates them.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Because Java has an automatic garbage collection algorithm that cleans up unused memory

How Does the Garbage Collector Work?

You just can't be sure. You might hear that the garbage collector uses a mark and sweep algorithm, and for any given Java implementation that might be true, but the Java specification doesn't guarantee any particular implementation. You might hear that the garbage collector uses reference counting; once again maybe yes maybe no. The important concept to understand for the exam is when does an object become eligible for garbage collection? To answer this question fully, we have to jump ahead a little bit and talk about threads. (Don't worry, We will take a detailed look at Threads in future.) In a nutshell, every Java program has from one to many threads. Each thread has its own little execution stack. Normally, the programmer causes at least one thread to run in a Java program, the one with the main() method at the bottom of the stack. However, there are many really cool reasons to launch additional threads from your initial thread. In addition to having its own little execution stack, each thread has its own lifecycle. For now, all we need to know is that threads can be alive or dead. With this background information, we can now say with stunning clarity and resolve that an object is eligible for garbage collection when no live thread can access it.

Based on that definition, the garbage collector does some magical, unknown operations, and when it discovers an object that can't be reached by any live thread, it will consider that object as eligible for deletion, and it might even delete it at some point. When we talk about reaching an object, we're really talking about having a reachable reference variable that refers to the object in question. If our Java program has a reference variable that refers to an object, and that reference variable is available to a live thread, then that object is considered reachable. We'll talk more about how objects can become unreachable in the following section.

Can a Java application run out of memory? Yes. The garbage collection system attempts to remove objects from memory when they are not used. However, if you maintain too many live objects (objects referenced from other live objects), the system can run out of memory. Garbage collection cannot ensure that there is enough memory, only that the memory that is available will be managed as efficiently as possible.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

How Does the Garbage Collector Work?

You just can't be sure. You might hear that the garbage collector uses a mark and sweep algorithm, and for any given Java implementation that might be true, but the Java specification doesn't guarantee any particular implementation. You might hear that the garbage collector uses reference counting; once again maybe yes maybe no. The important concept to understand for the exam is when does an object become eligible for garbage collection? To answer this question fully, we have to jump ahead a little bit and talk about threads. (Don't worry, We will take a detailed look at Threads in future.) In a nutshell, every Java program has from one to many threads. Each thread has its own little execution stack. Normally, the programmer causes at least one thread to run in a Java program, the one with the main() method at the bottom of the stack. However, there are many really cool reasons to launch additional threads from your initial thread. In addition to having its own little execution stack, each thread has its own lifecycle. For now, all we need to know is that threads can be alive or dead. With this background information, we can now say with stunning clarity and resolve that an object is eligible for garbage collection when no live thread can access it.

Based on that definition, the garbage collector does some magical, unknown operations, and when it discovers an object that can't be reached by any live thread, it will consider that object as eligible for deletion, and it might even delete it at some point. When we talk about reaching an object, we're really talking about having a reachable reference variable that refers to the object in question. If our Java program has a reference variable that refers to an object, and that reference variable is available to a live thread, then that object is considered reachable. We'll talk more about how objects can become unreachable in the following section.

Can a Java application run out of memory? Yes. The garbage collection system attempts to remove objects from memory when they are not used. However, if you maintain too many live objects (objects referenced from other live objects), the system can run out of memory. Garbage collection cannot ensure that there is enough memory, only that the memory that is available will be managed as efficiently as possible.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The JVM is responsible for running all the java programs. It is also responsible for keeping the memory utilization of the system at its least. So, the JVM runs a scan through the memory searching for unused object references. If an object is not referred by any class or other objects, then the JVM decides that this object is no longer required and hence it deletes the object from memory, thereby cleaning up space which will be available for other programs to run.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is Implicit garbage collection in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

2 What is the purpose of garbage collection in java and when is it used?

Garbage collection prevents memory leaks. In Java, the Java Virtual Machine will garbage collect whenever there is memory that has no references.


What implicit modifiers interface methods have in java?

public


What is the difference between garbage collector and finalization?

When all references to an object is Java are gone, the garbage collector will come along and free up the memory used by that object.Every Java object has a method named finalize, which is called by the garbage collector when the memory for that object is being deallocated. "Finalization" is just a name given to the act of calling the finalize method during garbage collection.


How do you write a Program on garbage collector in java?

Garbage collection is an operation that happens automatically in Java. We cannot write programs to perform them. All we can do is call the system's implementation of the Garbage collector and hope that it would execute. "Runtime.gc();" Place the above piece of code in your code, if you want to invoke the garbage collector. Invoking the runtime's implementation of the gc does not guarantee the execution of the garbage collector. It may or may not run. The JVM decides on that.


Why java does not support destructor?

AMIT KUMAR3th Nov , 2014Java does not support destructors, since java supports the concept of garbage collection,hence the garbage collector is used to automatically free the space which has occupied by the program while running.Garbage collector calls the finalize() method which is defined in the object class. finalize() method is called once for each object.

Related questions

2 What is the purpose of garbage collection in java and when is it used?

Garbage collection prevents memory leaks. In Java, the Java Virtual Machine will garbage collect whenever there is memory that has no references.


Garbagecolllecter in java?

Yes, Java programming language has a Garbage collector for unused memory. and the best part about it is that it does it automatically. The Garbage Collector is built into the Java Virtual Machine, and will do automatic garbage collection for you. If you chose to compile your Java code down to native code (via a Java->native code compiler), then NO garbage collection is done for you.


How do you force garbage collection in java?

. Garbage collection cannot be forced. Calling System.gc() or Runtime.gc() is not 100 percent reliable, since the garbage-collection thread might defer to a thread of higher priority


How you can inforce the garbage collection?

Forcing Garbage CollectionFirst and foremost, unlike this paragraphs title, garbage collection cannot be forced. However, Java provides some methods that allow you to request that the JVM perform garbage collection.In reality, it is possible only to suggest to the JVM that it perform garbage collection. However, there are no guarantees the JVM will actually remove all of the unused objects from memory (even if garbage collection is run).


What is the garbage collection in java?

The garbage collector is a process that will search for unreachable objects - objects which can't be reached from the main program - and destroy them, thus reclaiming the space they use.


What implicit modifiers interface methods have in java?

public


Why is your computer temporarily freezing when you play games run on java?

It may be loading something. Also, Java technology uses something called "garbage collection", which may temporarily freeze the game - or whatever program you are running. Garbage collection is a process used to reclaim memory that was once assigned to some aspect of a program, but that is no longer used.


What is the difference between garbage collector and finalization?

When all references to an object is Java are gone, the garbage collector will come along and free up the memory used by that object.Every Java object has a method named finalize, which is called by the garbage collector when the memory for that object is being deallocated. "Finalization" is just a name given to the act of calling the finalize method during garbage collection.


How do you write a Program on garbage collector in java?

Garbage collection is an operation that happens automatically in Java. We cannot write programs to perform them. All we can do is call the system's implementation of the Garbage collector and hope that it would execute. "Runtime.gc();" Place the above piece of code in your code, if you want to invoke the garbage collector. Invoking the runtime's implementation of the gc does not guarantee the execution of the garbage collector. It may or may not run. The JVM decides on that.


How garbage collector works on anonymous class in java?

about the garbage collector it is in java and it is mainly responsible for dynamic memory manegement


Why java does not support destructor?

AMIT KUMAR3th Nov , 2014Java does not support destructors, since java supports the concept of garbage collection,hence the garbage collector is used to automatically free the space which has occupied by the program while running.Garbage collector calls the finalize() method which is defined in the object class. finalize() method is called once for each object.


What is java all about?

Java is a flexible language that is compiled into bytecodes allowing the same program to run on any system with a Java Virtual Machine. As a language it has memory garbage collection, type safe variables, object orientation from the ground up, and lots of supporting libraries and infrastructure.