Solved

re-new object

Posted on 2002-05-30
12
192 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
ejb wildfly example 2 27
Adding multiple JVM environments to RedHat 6 7 49
jsp error 6 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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