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?

jdauesAsked:
Who is Participating?
 
girionisCommented:
 >is the first mc garbage collected?

  As long as there are no more references to the first one then yes, the object pointed by the first mc will get gced.

  >is this poor programming practice?

  Not to the best of my knowledge. You are allowed to re-assign the mc as many times as you want.

  Hope it helps.
0
 
Jan LouwerensSoftware EngineerCommented:
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)
0
 
objectsCommented:
>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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
JonathanJonasCommented:
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
0
 
girionisCommented:
> 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!!
0
 
0xDEADBEEFCommented:
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
0
 
objectsCommented:
> 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.

0
 
Jan LouwerensSoftware EngineerCommented:
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.
0
 
girionisCommented:
 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.
0
 
jdauesAuthor Commented:
thanks gironis
and also thanks to jlouwere for helpful suggestions
0
 
jdauesAuthor Commented:
and thanks to all for their comments, didn't mean to slight anyone
0
 
girionisCommented:
 Thank you for the points. I am glad I helped :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.