Need to protect method-local object against GC?

Discussion in 'Java' started by Steve Brecher, Feb 10, 2007.

  1. class Foo<Integer> implements Collection<Integer> {...}

    class Bar {

    Foo<Integer> aFoo() {
    return new Foo<Integer>(...);
    }

    void method() {
    for (Integer i : aFoo())
    ...;
    }

    During the loop execution method will not have an instance of Foo on its
    stack, but only an instance of Iterator<Integer>, and thus I think that
    there is no instance of Foo that a garbage collector can see as live, and
    hence the instance created by aFoo is subject to destruction during the
    loop. Therefore it is necessary to write instead something like,

    myFoo = aFoo();
    for (Integer i : myFoo)
    ...;

    Right?

    --
    For mail, please use my surname where indicated:
    (Steve Brecher)
    Steve Brecher, Feb 10, 2007
    #1
    1. Advertising

  2. Steve Brecher <see.signature@end> wrote:
    > During the loop execution method will not have an instance of Foo on
    > its stack, but only an instance of Iterator...


    But the Iterator has to have stored, or be associated with by virtue of
    being a nested class, an instance of Foo. So the loop is "GC safe."

    --
    For mail, please use my surname where indicated:
    (Steve Brecher)
    Steve Brecher, Feb 10, 2007
    #2
    1. Advertising

  3. Steve Brecher wrote:
    > class Foo<Integer> implements Collection<Integer> {...}
    >
    > class Bar {
    >
    > Foo<Integer> aFoo() {
    > return new Foo<Integer>(...);
    > }
    >
    > void method() {
    > for (Integer i : aFoo())
    > ...;
    > }
    >
    > During the loop execution method will not have an instance of Foo on its
    > stack, but only an instance of Iterator<Integer>, and thus I think that
    > there is no instance of Foo that a garbage collector can see as live, and
    > hence the instance created by aFoo is subject to destruction during the
    > loop. Therefore it is necessary to write instead something like,
    >
    > myFoo = aFoo();
    > for (Integer i : myFoo)
    > ...;
    >
    > Right?


    I'm not sure about the distinction you are making. The second piece of
    code is just a longer, more explicit, version one.

    If the Iterator needs to reference some other object to do its job, it
    is the responsibility of the Iterator implementation to ensure it either
    has or can obtain that reference, not your worry when using the Iterator.

    If the Iterator has or can obtain a reference to some Foo then as long
    as the Iterator is reachable from some ongoing computation in an active
    thread that Foo object is also reachable.

    Patricia
    Patricia Shanahan, Feb 10, 2007
    #3
  4. Steve Brecher

    Mark Rafn Guest

    Steve Brecher <see.signature@end> wrote:
    >class Foo<Integer> implements Collection<Integer> {...}
    >class Bar {
    > Foo<Integer> aFoo() {
    > return new Foo<Integer>(...);
    > }
    >
    > void method() {
    > for (Integer i : aFoo())
    > ...;
    >}


    >During the loop execution method will not have an instance of Foo on its
    >stack, but only an instance of Iterator<Integer>, and thus I think that
    >there is no instance of Foo that a garbage collector can see as live, and
    >hence the instance created by aFoo is subject to destruction during the
    >loop.


    In general, an Iterator keeps a reference to it's underlying Collection. I
    suppose it's possible not to, but if that were the case, why would you care if
    the Foo were collected?

    >Therefore it is necessary to write instead something like,
    > myFoo = aFoo();
    > for (Integer i : myFoo)
    > ...;
    >Right?


    No. Unless your code needs a reference to the Foo, there's no reason for your
    code to maintain the reference to it. Think of it this way: if the Iterator
    needs the reference, it'll keep it without your help, and if it doesn't need
    it, then you don't care if it gets GC'd.
    --
    Mark Rafn <http://www.dagon.net/>
    Mark Rafn, Feb 10, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD

    Re: Protect local data files from download?

    Steve C. Orr, MCSD, Jul 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    304
    Steve Brecher
    Jul 4, 2003
  2. Travis Newbury
    Replies:
    1
    Views:
    314
    dorayme
    May 10, 2008
  3. tkevans
    Replies:
    0
    Views:
    2,531
    tkevans
    Feb 11, 2009
  4. Replies:
    7
    Views:
    159
  5. Jean-Michel Pichavant
    Replies:
    0
    Views:
    169
    Jean-Michel Pichavant
    Oct 16, 2012
Loading...

Share This Page