Solved

re-new object

Posted on 2002-05-30
12
206 Views
Last Modified: 2010-03-31
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
Comment
Question by:jdaues
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 7045464
 >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
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7045624
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
 
LVL 92

Expert Comment

by:objects
ID: 7045636
>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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 2

Expert Comment

by:JonathanJonas
ID: 7045640
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
 
LVL 35

Expert Comment

by:girionis
ID: 7045650
> 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
 

Expert Comment

by:0xDEADBEEF
ID: 7047373
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
 
LVL 92

Expert Comment

by:objects
ID: 7047965
> 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
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7048030
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
 
LVL 35

Expert Comment

by:girionis
ID: 7048046
 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
 

Author Comment

by:jdaues
ID: 7048247
thanks gironis
and also thanks to jlouwere for helpful suggestions
0
 

Author Comment

by:jdaues
ID: 7048248
and thanks to all for their comments, didn't mean to slight anyone
0
 
LVL 35

Expert Comment

by:girionis
ID: 7048932
 Thank you for the points. I am glad I helped :-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question