Link to home
Start Free TrialLog in
Avatar of jdaues
jdaues

asked on

re-new object

Consider code something like this:

MyClass mc = new MyClass();
mc.setSomething(1);
mc.setAnotherThing(2);

...use mc somehow...

// then
mc = new MyClass();
mc.setSomething(3);
mc.setAnotherThing(4);

is the first mc garbage collected?
is this poor programming practice?

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unless there is some kind of function in that class that can reset the object's state to the same state as when it's first created, there is no other way to do what you wish.
Obviously, if there is such a function, it would be more efficient, both memory wise (not creating a new object) and execution wise (garbage collector won't have to run on that object)
However, most classes to not provide such a function, so you're only alternative is to do what you did, which is not bad.

If you are writing the class yourself, and you're going to be creating lots of objects, you might want to write a reset() method that can reset the object to an initial state (this method could also be called by the contructor)
>is the first mc garbage collected?

If no references exist to it, the it is *available* for gc. It is not necessarily gc'ed, it is up to the VM *when* the actual gc occurs.

>is this poor programming practice?

Nope.
Avatar of JonathanJonas
JonathanJonas

It's okay to re-assign the reference to the object, as long as you know that's what's going to happen and you keep in mind that the first object is going to be garbage collected.

For example if you have a Vector full of objects that you want to do something with one at a time you'd do something like:

for(int i = 0; i < vector.size(); i++)
{
    FooBar fooBar = (FooBar)vector.get(i);
    //Do stuff with fooBar
}

Or more generally if you were using the constructor each time, I.e. if you had a unique id which you were using to look up an object.

for(int i = 0; i < vector.size(); i++)
{
    String id = (String)vector.get(i);
    FooBar fooBar = new FooBar( id );
    //Do stuff with fooBar

}

Cheers,

Jonathan
> If no references exist to it, the it is *available* for gc.
>It is not necessarily gc'ed, it is up to
the VM *when* the actual gc occurs.

  Hmmm... Objects is right. It is not garbage collected but *eligible* for garbage collection. It might not get garbage collected at all during a programme's lifetime!!
is the first mc garbage collected?

Not necessarily by default. The gc is called by the VM usually. You can force a garbage collection, using the System.gc() command, but this is generally not advisable. The GC does take some time, and the VM handles this quite well.

is this poor programming practice?

In java, you don't have to free the memory manually. That's what the GC is for. However, I consider it bad style to use the same variable over and over again. This is usually hard to read and maintain. There are exceptions though, e.g. when using loops this may be necessary.
Anyway, I think style is a matter of belief. I for example like readable and easy-to-maintain ravioli code. Other people rather gain speed at the expense of maintainability.

For an explanation of the phrase 'ravioli code' and the complete pasta theory of programming see http://www.gnu.org/fun/jokes/pasta.code.html
> You can force a garbage collection using the System.gc() command

I'm not sure if this actually forces gc, I think it's just a suggestion to the VM that it should.

You are correct, objects. It's only a suggestion to the VM that it should execute garbage collection. There is no way to force it.
 According to the System javadoc for gc:

Calling the gc method suggests that the Java Virtual
 Machine expend effort toward recycling unused objects in order to
 make the memory they currently occupy available for quick reuse.
 When control returns from the method call, the Java Virtual
 Machine has made a best effort to reclaim space from all unused
 objects.
Avatar of jdaues

ASKER

thanks gironis
and also thanks to jlouwere for helpful suggestions
Avatar of jdaues

ASKER

and thanks to all for their comments, didn't mean to slight anyone
 Thank you for the points. I am glad I helped :-)