Declaring outside a loop: speed? memory?

P

Philipp

Hello
Just wondering, is there a speed or memory difference between the two
following codes? What is recommended? Which will easier garbage collect
(suppose MyClass is big)?

--- code ---
for(int i = 0; i < 1000; i++){
MyClass mine = new MyClass();
mine.doSomething();
}
------------
and
--- code ---
MyClass mine;
for(int i = 0; i < 1000; i++){
mine = new MyClass();
mine.doSomething();
}
 
S

Steve W. Jackson

Ben Caradoc-Davies said:
Funny you should ask, because it was just mentioned on Planet Classpath
http://planet.classpath.org/

Local Variables in Java
http://rmathew.blogspot.com/2007/01/local-variables-in-java.html

In short, there is no difference. The JVM specification requires them to
be the same, and the same bytecode is generated.

Given the specific code sample cited, I don't think there's enough
information to say that with certainty. Since it was omitted from the
reply, I'll paste it here:

--- code ---
for(int i = 0; i < 1000; i++){
MyClass mine = new MyClass();
mine.doSomething();
}
------------
and
--- code ---
MyClass mine;
for(int i = 0; i < 1000; i++){
mine = new MyClass();
mine.doSomething();
}
------------

In terms of speed, yes, it should be the same. In terms of memory,
there may be a difference depending on what else might follow the loop
in the second snippet.

Declaring the variable "mine" before the loop makes its scope wider than
just the loop. As a result, the last instance after the loop terminates
is still in existence until the wider scope is finished. Only then is
it certain to be eligible for garbage collection, should the JVM need to
do so. So if there's a good deal that follows this loop, there could be
a benefit to the first approach.

Personally, I would tend (in situations like this one) to opt for a form
slightly more compact than that inside the loop:

new MyClass().doSomething();

= Steve =
 
D

Daniel Pitts

Steve said:
Declaring the variable "mine" before the loop makes its scope wider than
just the loop. As a result, the last instance after the loop terminates
is still in existence until the wider scope is finished. Only then is
it certain to be eligible for garbage collection, should the JVM need to
do so. So if there's a good deal that follows this loop, there could be
a benefit to the first approach.

Actually, I think the compiler/JVM is smart enough to make references
elegible for GC if the ref isn't used for the rest of the scope.
 
D

Doug Pardee

Steve said:
Declaring the variable "mine" before the loop makes its scope wider than
just the loop. As a result, the last instance after the loop terminates
is still in existence until the wider scope is finished. Only then is
it certain to be eligible for garbage collection, should the JVM need to
do so. So if there's a good deal that follows this loop, there could be
a benefit to the first approach.

There is no effective difference at run-time. The reference variable
exists for the entire duration of the method call, and it contains
whatever it contains. Unless you explicitly null it, whatever it
references is not eligible for garbage collection even though you can
no longer access it.

Daniel said:
Actually, I think the compiler/JVM is smart enough to make references
elegible for GC if the ref isn't used for the rest of the scope.

No, it doesn't do that because it would usually be a bunch of needless
overhead. It is the programmer's responsibility to null out any
references that are no longer needed if it is important that the
referenced object(s) be made available for garbage collection
immediately. Otherwise, the object(s) will be available for garbage
collection when the current method call ends.

Sun's term for local variables that have gone out of scope but have not
yet been deallocated is "invisible". See section A.3.3 of
http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top