Re: general performance question



Mike Schilling wrote:
Matt Humphrey wrote:
"Tobi" <TobiMc3@xxxxxxxxx> wrote in message
news:628a2617-ff4a-460a-9c50-661e7f424f2f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Is it *really* true that creating an object closer to where it will
be used by the code, rather than creating it and setting it to null,
makes a noticeable difference performance-wise?
Clearly there's no sense at any time in creating an object and then
setting it to null, so I think you're mixing up variable declaration
with object creation.

There's this, which might not be what the OP had in mind:


void method()
{
...
if (condition)
{
LargeObject lg = new LargeObject();
lg.doStuff()
// See discussion below
}
...
}

This has been discussed on this group, and the consensus is that the method's stack frame continues to point to the LargeObject, so that it can't be collected until the method returns. (It seems to me that the JVM should be free to null out the reference once it goes out of scope, or even if it's in scope but flow analysis makes it clear that it can't be used any more, but that was a minority opinion.) Thus it can make sense to replace the comment with

lg = null;

I don't think that is true. If you create an Object in a loop and then reassign another Object to the same reference in the loop, the first Object is eligible for garbage collection (and will be).

--

Knute Johnson
email s/nospam/knute/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem
.



Relevant Pages

  • Re: general performance question
    ... first Object is eligible for garbage collection. ... For a loop, the value set in the last iteration also leaks the ... How can it if the reference is created in the block? ... The method's stack frame isn't collected until the method exits; it has no notion of block scope. ...
    (comp.lang.java.programmer)
  • Re: Declaring outside a loop: speed? memory?
    ... the last instance after the loop terminates ... it certain to be eligible for garbage collection, ... references is not eligible for garbage collection even though you can ... elegible for GC if the ref isn't used for the rest of the scope. ...
    (comp.lang.java.programmer)
  • Re: argument returning
    ... because there is still a live reference to it. ... long as the variable is in scope, ... The object will only be eligible for garbage collection ... Matt Humphrey matth@xxxxxxxxxxxxxx http://www.iviz.com/ ...
    (comp.lang.java.programmer)
  • Re: Anonymous Methods scope question
    ... CheckBox created - because they have a reference, not a copy, of their ... scope and some variables from an outer scope. ... declared in a "foreach" loop only has a single instance for the whole ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: general performance question
    ... first Object is eligible for garbage collection. ... For a loop, the value set in the last iteration also leaks the ... How can it if the reference is created in the block? ... no notion of block scope. ...
    (comp.lang.java.programmer)