Re: Declaring outside a loop: speed? memory?



In article
<45b5fbb5$0$7993$5a62ac22@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Ben Caradoc-Davies <ben@xxxxxxxxxxxxx> wrote:

Philipp wrote:
Just wondering, is there a speed or memory difference between the two
following codes?

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 =
--
Steve W. Jackson
Montgomery, Alabama
.



Relevant Pages

  • Re: Searching into Non-Contiguous ranges in a columns
    ... slr = Cells.End.Row ... Set r = .Cells ... Loop While Not c Is Nothing And c.Address firstAddress ... I know my codes are from A5 to A635. ...
    (microsoft.public.excel.programming)
  • RE: Timing events in GNAT GPL 2006
    ... loop null; end loop; ... There are three things wrong with your codes. ... The protected procedure Inc_Clock has wrong parameter mode. ... non-local pointer cannot point to local object error at ...
    (comp.lang.ada)
  • Re: Sun Studio Express 3 compilers available for download
    ... in subroutines on arrays with passed in dimensions. ... The compiler has ... And F77 codes are ... need to worry about loop order. ...
    (comp.lang.fortran)
  • Re: Messaging question
    ... Inside the PacketReceived event, ... sometimes it will loop through 5 ... The only thing I can think of is before the codes finish ... it seems that before it finish executing the codes ...
    (microsoft.public.vb.general.discussion)
  • Re: Garbage collection question?
    ... In the example you present, the Object created at the top of each loop iteration becomes eligible for GC as soon as a new reference is assigned to variable o, *provided that* the old Object's reference has not been assigned to some object that persists across loop iterations. ... Even if different slots were used, the stack frame size is fixed at compile time, so an infinite loop will quickly have to start reusing variable slots, so you will not get a memory leak from this direction. ... When a thread returns from a method, its stack frame for that method invocation is popped, freeing all the memory occupied by the local variables of that method for that invocation. ...
    (comp.lang.java.programmer)