4 Replies Latest reply: Dec 24, 2012 1:58 PM by baftos RSS

    How could this code possibly work?

    827603
      This code comes from a book:
      public Enumeration<String> getKeys() {
              Vector<String> vString = new Vector<String>();
              Enumeration en = props.keys();
              while (en.hasMoreElements()) {
                  vString.add((String) en.nextElement());
              }
              return vString.elements();
          }
      When getKeys() exits and its stack is forgotten, or at least when the next function call is executed by the same thread that executes getKeys(), the reference, vString, will probably be overwritten. Granted that the Vector vString refers to lives on the heap, is it not subject to garbage collection anytime after getKeys() exits? How can the user be sure that the Enumeration<String> that getKeys() returns will actually iterate over anything useful? The demo program that the book author offers does work though.
        • 1. Re: How could this code possibly work?
          Kayaman
          user8252446 wrote:
          When getKeys() exits and its stack is forgotten
          Correct.
          Granted that the Vector vString refers to lives on the heap
          Correct, all objects live on the heap.
          is it not subject to garbage collection anytime after getKeys() exits?
          Depends on what is still referring to it. In this case, the Enumeration is, so it won't be garbage collected.
          How can the user be sure that the Enumeration<String> that getKeys() returns will actually iterate over anything useful?
          Since the Enumeration is still referring to the Vector, it'll iterate (or rather enumerate) over it quite well. It would be ridiculous if the data disappeared like this.
          Garbage collection doesn't collect things that are still being used.
          • 2. Re: How could this code possibly work?
            baftos
            Let me reformulate his question the way I understand it.
            Object create() {
              return new Object()
            }
            Elsewhere:
            Object obj = create();
            What is the mechnism or the specification that guarantees that between the return statement and the assignment statement the newly created object cannot be garbage collected?
            • 3. Re: How could this code possibly work?
              jschellSomeoneStoleMyAlias
              baftos wrote:
              What is the mechnism or the specification that guarantees that between the return statement and the assignment statement the newly created object cannot be garbage collected?
              Presumably the last bullet point of the first paragraph in the following is what you are referring to?

              http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.1

              It can't be assigned if it doesn't exist.

              Or stating that in a different way.
              1. Once computed (step before the last bullet) the value must be held somewhere.
              2. That somewhere is considered an active reference by the GC.
              3. So it can't be collected before the last bullet point arrives.