answersLogoWhite

0


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

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.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is garbage collector why you use garbage collector.it is possible to extend heap memory?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you extend the elbow with your biceps?

No, this is not possible. Elbows extend by means of the triceps brachii (triceps). You can try to flex your bicep as you extend your arm, but when itès fully extend its not possible.


Is it possible to extend an exisiting lot on the sim 2?

No


Is it possible to extend the date of payment of admission fee including hostel as the bank loan is under process?

Yes it is possible.


Is it possible to put a trampoline on a slope?

it might be possible if you extend the legs on the side of the trampoline slopin downwards to even it out and keep it level


Is it possible to extend a fiancee visa?

In a word--no! http://www.dixonimmigration.com/index.php?pid=2


The reason maintenance is important to engines of vehicle?

To extend the life of the engine for as long as possible.


Does Florida extend benefits to same-sex partners of state employees?

Yes, but only for the employees of these offices:Office of the Clerk and ComptrollerOffice of the Property AppraiserOffice of the Supervisor of ElectionsOffice of the Tax Collector


What physical feature made it possible for Rome to extend its influence to Africa?

numerous island chains


How can you add more space in my c drive from other drive?

it is not possible to extend the size of your c drive. it is possible when u r formatting your whole system,at that time you can increase and decrease the size of c drive.


Is it possible to extend a tourist time in Italy by leaving and returning again Ive heard you can go to Switzerland and return again?

um... i would say no...


How would you connect scientific notation with the layers of the earth?

The layers of the earth extend for miles. It would be possible to list those distances in scientific notation.


A sentence for the word extend?

"I tried to extend my sales to a new area." Is a sentence with the word extend.