answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: 2 What is the purpose of garbage collection in java and when is it used?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


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.


When does Garbage collection occur in java?

No one can force garbage collector to free memory, A java object is subjected to garbage collection when it can not be reached by the program in which it is being used. Programmer can request for garbage collection by System.gc() but JVM doesn't guarantee that is will start immediatelyActually, the specific method for GC and how it will be run is dependent on the JVM implementation being used.For instance, Sun (now Oracle)'s Hotspot JVM has several different GC designs, one of which (the RealTime engine) can be immediately triggered via a System.gc() call. Each type of GC will have different triggers and thresholds as to when it will be run, which allows the user to pick the GC option that they think will be least disruptive and most effective for their particular application.You must read the JVM documentation for your particular VM to see what available GC options there are, how each GC method works, and what the various thresholds are.


What is garbage collector why you use garbage collector.it is possible to extend heap memory?

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.When Does the Garbage Collector Run?The garbage collector is under the control of the JVM. The JVM decides when to run the garbage collector. From within your Java program you can ask the JVM to run the garbage collector, but there are no guarantees, under any circumstances, that the JVM will comply. Left to its own devices, the JVM will typically run the garbage collector when it senses that memory is running low. Experience indicates that when your Java program makes a request for garbage collection, the JVM will usually grant your request in short order, but there are no guarantees. I repeat, the JVM does not guarantee the execution of the garbage collector when you invoke it. It can execute it and opt to ignore your request totally because that's how it works. We cant do a thing about it.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.


What is Implicit garbage collection in java?

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 CollectorGarbage 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.

Related questions

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 garbage collection answer?

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.


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.


What is the need for garbage collection?

Assuming you mean garbage collection in computers: it is a method often used to reclaim memory, once it is no longer used. Note that garbage collection is not the only possible way to manage memory.


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.


Give the brief introduction of garbage collection?

Garbage collection in java is nothing but the process of freeing up unused memory from the JVM memory heap. Every time an object is created, it is allocated some space in the memory. Once the use for that object is over, that memory space needs to be released so that it can be used by other objects. In Java, this process is automated and the JVM takes care of doing this. Once in a while, a program that performs this garbage collection runs and frees up memory for other objects to use. All objects whose references are no longer used are cleared.


Why are you used new operator in main method in java?

new is used for memory allocation in java which on later automatically deallocated by garbage collector.


Why does java provide garbage collection?

The idea of garbage collection is to reclaim unused memory - getting rid of objects that are no longer used. Since in Java, the programmer doesn't have to explicitly de-allocate memory that has been allocated, that not only reduces programming effort, this also eliminates a cause of lots of errors - since an error in this respect, by the programmer, would most likely cause a so-called "memory leak".


When does Garbage collection occur in java?

No one can force garbage collector to free memory, A java object is subjected to garbage collection when it can not be reached by the program in which it is being used. Programmer can request for garbage collection by System.gc() but JVM doesn't guarantee that is will start immediatelyActually, the specific method for GC and how it will be run is dependent on the JVM implementation being used.For instance, Sun (now Oracle)'s Hotspot JVM has several different GC designs, one of which (the RealTime engine) can be immediately triggered via a System.gc() call. Each type of GC will have different triggers and thresholds as to when it will be run, which allows the user to pick the GC option that they think will be least disruptive and most effective for their particular application.You must read the JVM documentation for your particular VM to see what available GC options there are, how each GC method works, and what the various thresholds are.


In java garbage collection is a class or an object or anything else?

Garbage collection is an automatic memory management feature built into the JVM. During the course of program execution, it will find objects that are no longer being used and allow their memory allocations to be used in other parts of the program.As mentioned, the process is automatic. That means you don't have to worry about that part of your program. It is not a class or an object - just a part of the VM that runs "under the hood" as your program is executed.However, garbage collection runs periodically so as to be more efficient. If you want to call the garbage collector at a particular moment, the Runtime class allows access to the garbage collector. To invoke, call the following: Runtime.getRuntime().gc().


How do you destroy an array?

That depends on the programming language. In Java - since you posted the question in the Java category - an array is treated like an object. That means that you usually don't need to do anything: as soon as there are no more variables that point to the array - and that usually happens when the method that declares the array finishes running - it will become available to be destroyed automatically by the garbage collector. The programmer doesn't have much control over WHEN exactly this happens; the Java Virtual Machine decides when it's convenient to run the garbage collector.


For what purpose constructors are used in Java?

Constructors are used to create the instance of a class.