I had learned that JVM will collect only the freed memory location during the garbage collection..That is if an object had a reference, then it will not be collected...
My doubt is, will the JVM collect all the used location whenever it goes out of the method..That is .

public void aa(){
           ArrayList list = new ArrayList();
           for (int i = 0; i < 1000000; i++) {
	              list .add(i);
            }
}

when will the memory allocated for "list" will be collected by JVM..Will it be collected when moving out of the function aa()..

Recommended Answers

All 9 Replies

As soon as an object is no longer referenced it will be garbage collected (at the next GC run). If the only reference to that list was in that method, then yes, that list will be eligable to be GCed as soon as the method returns. Not that that method will work in that manner, as you can only add objects, not primitives to a collection, and since you did not generitise that list with <Integer>, autoboxing will not happen (the VM does not autobox to Object, it autoboxes to relevant Number types).

Okk..Thanks...
One more doubt..

1. When will the VM run GC..I know we can invoke the garbage collector using System.gc(); But without this when will it perform this..

2. Is there any need of destroying the "list" when unused, for making better memory management..i had read it somewhere

1. Periodically. When, exactly, is irrelevant and varies from platform to platform. It is an implementation detail of the VM that the programmer doesn't really need to concern himself with. Also, System.gc(), and Runtime.getRuntime().gc() are only "suggestions" to the the VM that now would be a good time to do GC. The VM does not have to follow it though. It may do it then, it may do it after a delay, or it may ignore it altogether.

2. No. That's "humbug" to quote Ebeneezer Scrooge. There are some cases where it may be helpful, but they are real corner cases (i.e. very, very, very little instances) and, as long as the programmer is doing his job right, should never be a worry.

commented: thats a very good point that every one should be adviced on the use of System.gc() +2

Thanksss a lot

Also just to add to what masijade has explained very well and that too in a very concise manner, calling System.gc() is not exactly considered a good programming practice.

Also one question comes to mind here, how do you think you are going to destroy the object ?

Object can be distroyed using::::

object = null;

right????


But what is the problem with GC....it is just to invoke garbage collector..But with this invoke the VM will not surely collect all the garbages....

No, that simply removes that one reference from the instance. The instance itself still exists until garbage collected (or not if anything still references it), you just can't get at it.

Calling gc (whether through System or Runtime) does not guarantee that garbage collection will performed as a result of it. It is simply a "suggestion". And, 99.99999% of the time it is completely unnecessary to call gc, as the JVM algorithm is very effecient, and more than capable for 99.99999% of all cases. (Okay, maybe not that high of a percentage, but you get my drift).

@ajithraj :
> Object can be distroyed using:::: object = null
No, as masijade explains, and as I was assuming you to have the destruction of an object in your mind. Thats the reason I asked you the question 'How are you going to destroy it'.

If "destroying" an object refers to freeing up the memory space associated with that object, as it should be, you can never destroy an object in Java. What you do by assigning a null value to a reference, is that you take a reference away from the object, is effect just decrementing it's reference count. The Garbage Collector will collect all objects that have a reference count as zero. So your assigning a 'null' value to the reference just announces on your part that that reference no longer needs that object, but there may however be other references that continue to refer to that object and hence the object may never get GCed.

To tell you about why calling System.gc() is not a recommended practice, masijade has already given you most of the answer, mentioning that calling System.gc(), is only a "suggestion" to the JVM that now would be a good time to do GC. But whether the JVM obeys you, acting like a good genie, or not is a completely different matter altogether. Also it has good reason to not to obey you if it does that, because JVMs today have highly efficient and effective algorithms when it comes to judging when the system should be GCed. Also after mentioning the fact that your calling System.gc() would not gaurantee an actual invocation of the GC, putting System.gc() would only give developers, who are oblivion of this fact, a false impression that their objects are getting collected periodically.

Thanks a lot.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.