exquisitus coughed up:
Thanks - but I don't think youve answered my question. Stated another
way, sgould I even bother setting objects to null after using them, or
shall I just leave them to go out of scope and get "alloced" memory to
get reclaimed by the GC?.
In practice, you will sometimes see people explicitly null out a reference
so that reclamation occurs sooner than would otherwise happen. This is
usually only for large costly objects, or for large numbers of objects where
the sum total is excessive.
One thing that you can do is call System.gc(), which basically /asks/ the GC
to run the garbage collector if it feels like it. Here is what the docs
(1.5.0) say for it:
<quote>
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 discarded objects.
</quote>
This, however, is very misleading, in that it implies that the GC actually
is required to do something. As it turns out, the gc()'s "best effort"
could well be to do nothing at all, if its algorithm so chose.
Is there any difference in performance, is
one way more efecient than the other, or is there no difference
(purely a matter of personal preference/style)?
You /can/ see a difference, depending upon the vagaries of the algorithm.
However, actually controlling the thing is not an option. Most folks I know
of will experiment with nulling out references before they go out of scope,
but their results are hardly worth the exercise unless the objects memory
consumption is very large. There are run-time flags for modifying this
behavior as well, but I've never had to use them.
Whilst we are at it, is it true that declared (but not initialized)
objects are set to null by default (in which case I can do a check for
nulls without having made the assignement myself.
Yes.
Also, is there a default value fo declared primitives and elements of
an Array of primitives/array of objects.
Null for objects. 0 for primitives.
Many questions, I appreciate
your patience - look forward to your response.
Thanks
--
Unix users who vehemently argue that the "ln" command has its arguments
reversed do not understand much about the design of the utilities. "ln
arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
Existing file argument to non-existing argument. And in fact, mv
itself is implemented as a link followed by an unlink.