• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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?

0
jdaues
Asked:
jdaues
  • 4
  • 2
  • 2
  • +3
1 Solution
 
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 LouwerensCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 LouwerensCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now