Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Re: garbage collection
Re: garbage collection [message #88622] Sat, 23 August 2003 17:12 Go to next message
Eclipse UserFriend
I believe running GC inside the VM may help. The reason is finalization.

When the GC finds an unreachable object for the first time, it only
tries to collect it. If the object has a finalizer it will not be
collected and the finalizer will be invoked at some later time (the
finalizer may even have the object resurrected)! Only when the GC runs
for the second time it can collect the object (provided the finalizer
has run and the object was not resurrected).

Therefore if an object has a finalizer, it may take two GC runs to
collect it. I believe the JVM only runs the GC once before throwing
OutOfMemoryException. Therefore it may be useful to run the GC from time
to time (notice that it *may* be useful, i do not claim it is useful in
the common case)

Here is a test program that demonstrates it (description is below):

package test;

/**
* Copyright (C) 2003 Genady Beryozkin
*/
public class TestVM {

byte[] bigData;
String name;

public TestVM(String name) {
this.name = name;
bigData = new byte[1000*1000*40];
}

protected void finalize() throws Throwable {
System.out.println(name + " says: Hey - My finalizer is being run!!!");
}

public static void main(String[] args) {
System.out.println("allocating first object");
TestVM test = new TestVM("first object");
System.out.println("allocated first object");

// clearing the reference - allowing GC to occur
test = null;

// uncomment the following line to make the program succeed
//System.gc();

System.out.println("allocating second object");
test = new TestVM("second object");
System.out.println("allocated second object successfully");
}
}


In this program I allocate a large object (40Mb) twice. The size of the
object ensures that only one such object can fit into the default heap
size (64Mb). It also has a finalizer, that prevents it from being
collected during the first collection.

I allocate one such object and then nullify its reference. Notice that
there is no reason for the GC to run. When I try to allocate the second
object, the GC must run to try to free some space. But, it cannot -
because we have a finalizer that we must run. So the GC fails, and the
program aborts with an OutOfMemoryException.
The transcript of running the program with JDK 1.4.2 on WindowsXP SP1
with "-verbose:gc" argument is below:

> trying to allocate first object
> [GC 132K->88K(1984K), 0.0044274 secs]
> [Full GC 88K->88K(1984K), 0.0096283 secs]
> allocated first object
> trying to allocate second object
> [GC 39151K->39150K(41048K), 0.0017835 secs]
> [Full GC 39150K->39150K(41048K), 0.0100488 secs]
> [Full GC 39150K->39150K(65088K), 0.0678346 secs]
> first object says: Hey - My finalizer is being run!!!
> java.lang.OutOfMemoryError
> Exception in thread "main"


If I uncomment the "System.gc()" line, I force the garbage collector to
run before I attempt to allocate the second object. Therefore, the
finalizer runs *before* I attempt to allocate the second object, and
when I attempt to allocate it - the GC runs for the second time, and
this time it can collect the first object and make space for the second
object. Now the second object can be successfully allocated. The
transcript of the session is below:

> trying to allocate first object
> [GC 132K->88K(1984K), 0.0044925 secs]
> [Full GC 88K->88K(1984K), 0.0096747 secs]
> allocated first object
> [Full GC 39150K->39150K(41048K), 0.0101943 secs]
> first object says: Hey - My finalizer is being run!!!
> trying to allocate second object
> [GC 39151K->39150K(65088K), 0.0011029 secs]
> [Full GC 39150K->88K(65088K), 0.0255519 secs]
> allocated second object successfully

Genady

Dave Wegener wrote:

> Gernot Veith wrote:
>
>
>>hi,
>
>
>>sometimes when I've worked on a large project for some hours, I get an
>>out of memory message. AFAIK some IDEs supply a button to enforce
>>garbage collection. Eclipse does not so far. Whould that be practicable
>>? Currently I have to restart eclipse.
>
>
>>Gernot
>
> Running GC will not help. The VM will automatically perform a GC prior to
> giving an Out of Memory error. The GC can only free up unused memory. An
> Out of Memory error means that there is not enough free memory left to
> honor the request. You need to give the VM more memory when starting up
> Eclipse. Search the help for command line options or the news groups for
> out of memory threads in order to get the exact syntax for specifying
> additional memory.
>
Re: garbage collection [message #88637 is a reply to message #88622] Sat, 23 August 2003 17:16 Go to previous message
Eclipse UserFriend
I belive it may interest you ...

Genady wrote:

> I believe running GC inside the VM may help. The reason is finalization.
>
> When the GC finds an unreachable object for the first time, it only
> tries to collect it. If the object has a finalizer it will not be
> collected and the finalizer will be invoked at some later time (the
> finalizer may even have the object resurrected)! Only when the GC runs
> for the second time it can collect the object (provided the finalizer
> has run and the object was not resurrected).
>
> Therefore if an object has a finalizer, it may take two GC runs to
> collect it. I believe the JVM only runs the GC once before throwing
> OutOfMemoryException. Therefore it may be useful to run the GC from time
> to time (notice that it *may* be useful, i do not claim it is useful in
> the common case)
>
> Here is a test program that demonstrates it (description is below):
>
> package test;
>
> /**
> * Copyright (C) 2003 Genady Beryozkin
> */
> public class TestVM {
>
> byte[] bigData;
> String name;
>
> public TestVM(String name) {
> this.name = name;
> bigData = new byte[1000*1000*40];
> }
>
> protected void finalize() throws Throwable {
> System.out.println(name + " says: Hey - My finalizer is being
> run!!!");
> }
>
> public static void main(String[] args) {
> System.out.println("allocating first object");
> TestVM test = new TestVM("first object");
> System.out.println("allocated first object");
>
> // clearing the reference - allowing GC to occur
> test = null;
>
> // uncomment the following line to make the program succeed
> //System.gc();
>
> System.out.println("allocating second object");
> test = new TestVM("second object");
> System.out.println("allocated second object successfully");
> }
> }
>
>
> In this program I allocate a large object (40Mb) twice. The size of the
> object ensures that only one such object can fit into the default heap
> size (64Mb). It also has a finalizer, that prevents it from being
> collected during the first collection.
>
> I allocate one such object and then nullify its reference. Notice that
> there is no reason for the GC to run. When I try to allocate the second
> object, the GC must run to try to free some space. But, it cannot -
> because we have a finalizer that we must run. So the GC fails, and the
> program aborts with an OutOfMemoryException.
> The transcript of running the program with JDK 1.4.2 on WindowsXP SP1
> with "-verbose:gc" argument is below:
>
>> trying to allocate first object
>> [GC 132K->88K(1984K), 0.0044274 secs]
>> [Full GC 88K->88K(1984K), 0.0096283 secs]
>> allocated first object
>> trying to allocate second object
>> [GC 39151K->39150K(41048K), 0.0017835 secs]
>> [Full GC 39150K->39150K(41048K), 0.0100488 secs]
>> [Full GC 39150K->39150K(65088K), 0.0678346 secs]
>> first object says: Hey - My finalizer is being run!!!
>> java.lang.OutOfMemoryError
>> Exception in thread "main"
>
>
>
> If I uncomment the "System.gc()" line, I force the garbage collector to
> run before I attempt to allocate the second object. Therefore, the
> finalizer runs *before* I attempt to allocate the second object, and
> when I attempt to allocate it - the GC runs for the second time, and
> this time it can collect the first object and make space for the second
> object. Now the second object can be successfully allocated. The
> transcript of the session is below:
>
>> trying to allocate first object
>> [GC 132K->88K(1984K), 0.0044925 secs]
>> [Full GC 88K->88K(1984K), 0.0096747 secs]
>> allocated first object
>> [Full GC 39150K->39150K(41048K), 0.0101943 secs]
>> first object says: Hey - My finalizer is being run!!!
>> trying to allocate second object
>> [GC 39151K->39150K(65088K), 0.0011029 secs]
>> [Full GC 39150K->88K(65088K), 0.0255519 secs]
>> allocated second object successfully
>
>
> Genady
>
> Dave Wegener wrote:
>
>> Gernot Veith wrote:
>>
>>
>>> hi,
>>
>>
>>
>>> sometimes when I've worked on a large project for some hours, I get an
>>> out of memory message. AFAIK some IDEs supply a button to enforce
>>> garbage collection. Eclipse does not so far. Whould that be
>>> practicable ? Currently I have to restart eclipse.
>>
>>
>>
>>> Gernot
>>
>>
>> Running GC will not help. The VM will automatically perform a GC
>> prior to
>> giving an Out of Memory error. The GC can only free up unused
>> memory. An
>> Out of Memory error means that there is not enough free memory left to
>> honor the request. You need to give the VM more memory when starting up
>> Eclipse. Search the help for command line options or the news groups for
>> out of memory threads in order to get the exact syntax for specifying
>> additional memory.
>>
>
Previous Topic:[ANNOUNCE] Abbot for SWT 0.9.0
Next Topic:Error Copying Resources
Goto Forum:
  


Current Time: Mon Jul 21 23:56:53 EDT 2025

Powered by FUDForum. Page generated in 0.05054 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top