And thus spoke dalouis...
> And thus spoke dalouis...
>
[quoted text clipped - 5 lines]
>
> Flo
Yes looking at it again this might be somewhat obvious but what might
not be obvious is that in (b) there is likely to be a re-allocation of
memory on every iteration of that loop. The memory space for that
array will be marked as unused at the end of each iteration and then at
the beginning of the iteration it will be reassigned a new memory
location.
in (a) while the data is overwritten it will be written to the same
memory location each time.
Java may be smart enough to fix this in (b) but more likely it will
garbage collect the old memory and then reassign a new memory spot.
Flo 'Irian' Schaetz - 26 Dec 2006 19:15 GMT
And thus spoke dalouis...
> in (a) while the data is overwritten it will be written to the same
> memory location each time.
Are you sure? Is this somewhere in the language spec? Otherwise I
wouldn't know why ...
Indi[] i;
i = new Indi[x];
i = new Indi[x];
... should use the exact same memory location for the array both times?
If the garbage collector didn't start, the first array will still
"exist" somewhere (afaik, of course). And why if somewhere in the loop
is an...
(static) Indi xyz = i[12];
...? Perhaps I'm wrong, but I think it would be pure luck if the array
goes to the same memory location in every loop cycle in a).
Flo
Oliver Wong - 27 Dec 2006 23:29 GMT
> And thus spoke dalouis...
>
[quoted text clipped - 9 lines]
>
> ... should use the exact same memory location for the array both times?
Maybe the first instance of Indi might still be
usable/reachable/whatever when the constructor is invoked the second time.
I'm not sure how "magical" arrays are treated in Java, but if their
behaviour is specified in some class file, perhaps the JVM cannot assume
that the array constructor might not save a reference to itself in some sort
of static cache, for example.
- Oliver
Doug Pardee - 27 Dec 2006 23:37 GMT
> in (b) there is likely to be a re-allocation of memory on
> every iteration of that loop.
No, that cannot happen. The Java bytecode has no instructions for doing
that. Allocation of stack memory, including local variables, occurs
only during the method call operation.
An interesting side effect: the variables are also not DE-allocated
until the return from the method. As a result, memory referenced by
variables which were declared in nested blocks but have gone out of
scope will not be garbage collected until the method returns.
Theoretically, the compiler COULD automatically stick in code to set
the variable to null when the block is exited, but there are many
practical reasons not to. Not the least of which is that you could (and
should) do it yourself if you really care. 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