participate


Java Programming [Archive] - garbage collection dilema
<<   Back to Forum  |   Give us Feedback
This topic has 6 replies on 1 page.
gregwk0clemson
Posts:4
Registered: 2/7/03
garbage collection dilema   
Sep 21, 2003 3:41 PM

 

Dear Java programmers,

Can anyone tell me why I am getting an out-of-memory error with the
code below?

I am using a 'swapable' object (SObject), whose instance variable (an
'int') is buried inside an inner 'Rep' class. The extra level of
indirection allows the call x.swap(y) to exchange the integer values
of x and y. The container class (SArray) has a method that allows a
swapable object to exchange integer values with an object inside the
container. For example, s.swapAt(x, i) will cause the integer value
of x to be exchanged with the object at location i in s.

The program shown below uses a for-loop to populate the SArray with
one million SObjects. It does this using the 'insert' method which is
implemented with assignment. The next for-loop overwrites those one
million objects with new objects. This produces a million memory
leaks, but the garbage collector handles them. The final for-loop
'swaps' one million new objects with the million objects put there by
the second loop. This is where the program runs out of memory. So I
get the following output:

Done with initial input
Done with assignment overwrite
Exception in thread "main" java.lang.OutOfMemoryError

I was under the impression the the garbage collector kicked in when
memory was low and cleaned things up. At any given time during the
third loop there are only a little over a million objects in memory
that are accessible - everything else it garbage. So wouldn't the
garbage collector be able to reclaim the memory from these leaks? I
expect the third loop to take more time because of the extra garbage,
but I do not understand why the program would run out of memory.

Any information on why this is occuring or how it might be prevented
would be helpful.

Greg K.


package swaptest;
 
public class Main {
    private static final int MAX = 1000000;
 
    public static void main(String[] args) {
        SArray s = new SArray(MAX);
 
        for (int i = 0; i < MAX; i++) { s.insertAt(new SObject(), i); }
        System.out.println("Done with initial input");
        for (int i = 0; i < MAX; i++) { s.insertAt(new SObject(), i); }
        System.out.println("Done with assignment overwrite");
        for (int i = 0; i < MAX; i++) { s.swapAt(new SObject(), i); }
        System.out.println("Done with swapping overwrite");
    }
}
 
package swaptest;
 
public class SArray {
    private SObject[] contents;
 
    public SArray(int max) { contents = new SObject[max]; }
    public void swapAt(SObject x, int i) { contents[i].swap(x); }
    public void insertAt(SObject x, int i) { contents[i] = x; }
}
 
package swaptest;
 
public class SObject {
    public class Rep { public int n; }
    public Rep rep = new Rep();
 
    public SObject() { rep.n = 0; }
 
    public void swap(SObject x) {
        Rep temp = rep;
        rep = x.rep;
        x.rep = temp;
    }
}
 
 
javatypo
Posts:759
Registered: 9/14/01
Re: garbage collection dilema   
Sep 21, 2003 5:24 PM (reply 1 of 6)  (In reply to original post )

 
well


1000000 is alot of objects
 
bsampieri
Posts:14,508
Registered: 4/2/99
Re: garbage collection dilema   
Sep 21, 2003 6:42 PM (reply 2 of 6)  (In reply to #1 )

 
add more memory to the JVM:

java -Xms64m -Xmx256m someclass
 
gregwk0clemson
Posts:4
Registered: 2/7/03
Re: garbage collection dilema   
Sep 21, 2003 6:52 PM (reply 3 of 6)  (In reply to #1 )

 

1000000 is alot of objects

Good point. So I guess my real question is: Why would the second loop handle a million objects, but the third loop breaks with a million objects?

The second loop breaks around 1.85 million objects, and the third loop breaks around a million (even when you switch the order of the loops).
 
jbartel2
Posts:12
Registered: 12/20/02
Re: garbage collection dilema   
Sep 22, 2003 6:22 AM (reply 4 of 6)  (In reply to original post )

 
private static final int MAX = 1000000;
...
SArray s = new SArray(MAX);


That is a big object by itself and will not be eligible for gc until main is exited.

new SObject()


This creates 2 objects, the SObject and the Rep object that SObject contains.

The swap method in SObject is also creating a new Rep object each time it is called.

Did you try putting each loop in its own method (each with their own SArray) and calling the three methods from main to see if that would solve the out of memory problem?
 
Pete_Kirkham
Posts:599
Registered: 8/6/03
Re: garbage collection dilema   
Sep 22, 2003 12:24 PM (reply 5 of 6)  (In reply to original post )

 
Depending on which compiler you're using, you may have to explicitly make the Rep inner class static to prevent it holding a reference to its outer SObject.


Pete
 
gregwk0clemson
Posts:4
Registered: 2/7/03
Re: garbage collection dilema [solved]   
Sep 22, 2003 6:46 PM (reply 6 of 6)  (In reply to #5 )

 
Depending on which compiler you're using, you may have
to explicitly make the Rep inner class static to
prevent it holding a reference to its outer SObject.


Pete


Bingo!

So the following was happening to me:

Before picture: Cell 47 of an SArray points to Rep A (through an
SObject). But I want it to point to Rep B, so I swap references to
Reps A and B through the SObjects that created them.


  +--+
  |  |
  +--+     +--+ <---+---+
  |47|---> |  |---> | A |
  +--+     +--+     +---+
  |  |
  +--+     +--+     +---+
           |  |---> | B |
           +--+ <---+---+
 


After picture: Now cell 47 correctly points to Rep B. But Reps A and
B still hold references to the SObjects that created them. All
objects are accessible through cell 47, so no memory can be reclaimed
after the swap.


  +--+
  |  |
  +--+     +--+ <---+---+
  |47|---> |  |   ->| A |
  +--+     +--+\ /  +---+
  |  |          X 
  +--+     +--+/ \  +---+
           |  |   ->| B |
           +--+ <---+---+
 


Making the Rep class static solves the problem.

Thanks Pete!
Greg K.
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 63
  • Guests : 118

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums