Quick garbage collection question

OK, I have a (hopefully) quick question for someone. Basically my application has been reporting java.lang.OutOfMemory error, and I think it is because I was creating instances of classes quite regularly and they wernt being GC'd. Here is what I was doing:

I was periodically calling, simply:

privatevoid method myMethod(){

new myClass();

}

does this mean that, this created an object with no reference to it, and therefore would remain in memory indefinately?

I am now doing:

privatevoid method myMethod(){

myClass InstanceOfClass =new myClass();

}

will this mean that everytime the new method is called, the previous instance of myClass is de-referenced and then garbage collected?

my application calls myMethod() regularly.

Thanks

Andy

[1129 byte] By [djroj2001a] at [2007-10-3 4:53:04]
# 1

> OK, I have a (hopefully) quick question for someone.

> Basically my application has been reporting

> java.lang.OutOfMemory error, and I think it is

> because I was creating instances of classes quite

> regularly and they wernt being GC'd. Here is what I

> was doing:

>

> I was periodically calling, simply:

>

> [code]

> private void method myMethod(){

>new myClass();

> ode]

>

> does this mean that, this created an object with no

> reference to it, and therefore would remain in memory

> indefinately?

>

> I am now doing:

>

> [code]

> private void method myMethod(){

>myClass InstanceOfClass = new myClass();

> ode]

>

> will this mean that everytime the new method is

> called, the previous instance of myClass is

> de-referenced and then garbage collected?

>

> my application calls myMethod() regularly.

>

> Thanks

> Andy

When the method ends, if that's all you're doing there is no longer a reference to MyClass and it'll be marked for GC.

When you create a new MyClass are you spinning off a new thread? If you are then you might be getting OOM from a thread overflow.

Norweeda at 2007-7-14 22:57:54 > top of Java-index,Java Essentials,Java Programming...
# 2

Ahh I see... thats bad new (have to work out whats causing it now):

Well I think it must be spinning off another thread regularly, because netBeans output window was reporting:

Thread: Timer-105 java.lang.OutOfMemory

Thread: Timer-106 java.lang.OutOfMemory

Thread: Timer-107 java.lang.OutOfMemory

Thread: Timer-108 java.lang.OutOfMemory

Thread: Timer-109 java.lang.OutOfMemory

etc....

this is the class I am creating. Obviously it's not actually called myClass:

public class updateDelay {

Timer timer;

mainScreen statPanel = null;

public updateDelay(mainScreen panel) {

statPanel = panel;

timer = new Timer();

timer.schedule(new RemindTask(), 500);

}

class RemindTask extends TimerTask {

public void run() {

statPanel.loadStats();

statPanel.setTime();

timer.cancel();

}

}

}

It doesnt appear to create a new thread, unless extending Timer creates a thread?

Any ideas?

Thanks again.

djroj2001a at 2007-7-14 22:57:54 > top of Java-index,Java Essentials,Java Programming...
# 3

> OK, I have a (hopefully) quick question for someone.

> Basically my application has been reporting

> java.lang.OutOfMemory error, and I think it is

> because I was creating instances of classes quite

> regularly and they wernt being GC'd. Here is what I

> was doing:

>

> I was periodically calling, simply:

>

> [code]

> private void method myMethod(){

>new myClass();

> ode]

>

> does this mean that, this created an object with no

> reference to it, and therefore would remain in memory

> indefinately?

>

> I am now doing:

>

> [code]

> private void method myMethod(){

>myClass InstanceOfClass = new myClass();

> ode]

>

> will this mean that everytime the new method is

> called, the previous instance of myClass is

> de-referenced and then garbage collected?

>

> my application calls myMethod() regularly.

>

> Thanks

> Andy

Apparently the reference to the newly instantiated object is not escaping so it would be out of scope outside the method being local, so would be ready for garbage collection as soon as the method finishes, unless you are starting a thread in the constructor, because in such a case the reference escapes.

Starting a thread in a constructor is a big no no.

kilyasa at 2007-7-14 22:57:54 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi,

you're creating a new Timer object each time you call updateDelay. Each Timer has a thread to execute the timer tasks. This seems to cause the problem. Instead you should have a single Timer object for all tasks.

Of course, you have to remove the timer.cancel call in your RemindTask in order not to cancel other tasks pending in the same Timer. But I don't see any need for that anyway: when the TimerTask is executed, it is already removed from the task list of the Timer.

m.wintera at 2007-7-14 22:57:54 > top of Java-index,Java Essentials,Java Programming...
# 5

Oh I see... thanks. So I've just done a quick modification:

1. only one instance of Timer now.

public class updateDelay {

Timer timer = new Timer();

mainScreen statPanel = null;

public updateDelay(mainScreen panel) {

statPanel = panel;

timer.schedule(new RemindTask(), 500);

}

class RemindTask extends TimerTask {

public void run() {

statPanel.loadStats();

statPanel.setTime();

}

}

}

This will stop the creation of new threads right?

Cheers

djroj2001a at 2007-7-14 22:57:54 > top of Java-index,Java Essentials,Java Programming...
# 6
could someone confirm my above post. Thanks.need to press on with development
djroj2001a at 2007-7-14 22:57:54 > top of Java-index,Java Essentials,Java Programming...