9 Replies Latest reply: Oct 2, 2012 3:27 AM by jwenting RSS

    Garbage collection

    SRAVZ
      Hi All

      I have the following code:

      Map abc = new Hashmap();
      abc.put(1,1);
      abc.put(2,1);
      int xyz = Method(abc);
      //Garbage collection
      if(abc!=null){
      abc = null;
      }

      pubic int Method(Hashmap abc){
      Methods to add hashmap keys n return the value.
      }


      Question:
      1. Is there a benefit in doing this
      if(abc!=null){
      abc = null;
      }

      Thanks
      Sravz
        • 1. Re: Garbage collection
          EJP
          Usually not. A local variable will fall out of scope anyway, and a member variable should generally have the same lifetime as the object it is a member variable of.
          • 2. Re: Garbage collection
            SRAVZ
            EJP

            Usually not? - Can it happen rarely?

            Local Variable:- if the local variable is out of scope, then it would be garbage collected?

            Member variable:- If the hashmap is is a member variable, will hashmap is made as null what happens, .i.e. will it release space of the values stored and will size of the variable gets reduced?

            I know if a variable is made as null, it would be not be part of heap space -- is it true?

            Thanks
            Sravz
            • 3. Re: Garbage collection
              EJP
              Usually not? - Can it happen rarely?
              There are circumstances in which it makes sense to code that way. They are rare. That's what 'usually not' means actually.
              Local Variable:- if the local variable is out of scope, then it would be garbage collected?
              If that was the only or last and only reference, the object referred to becomes eligible for GC.
              Member variable:- If the hashmap is is a member variable, will hashmap is made as null what happens
              The HashMap becomes eligible for GC as above.
              will it release space of the values stored
              If those were the only or last and only references, they become eligible for GC as above.
              and will size of the variable gets reduced?
              Size of what variable?
              I know if a variable is made as null, it would be not be part of heap space -- is it true?
              You are confusing variables with the objects they point to. Variables are on the stack if local, otherwise in the heap. The objects they reference are always in the heap, and once they are GCd they no longer exist anywhere, let alone in the heap.
              • 4. Re: Garbage collection
                SRAVZ
                Thanks EJP, its getting clear now.

                Usually not? - Can it happen rarely?
                There are circumstances in which it makes sense to code that way. They are rare. That's what 'usually not' means actually.

                Just curious to know what are the condition does that happen?

                Is there some documentation you can point me too? I am golfing to find some articles.

                Thanks
                Sravz
                • 5. Re: Garbage collection
                  EJP
                  Just curious to know what are the condition does that happen?
                  There are no conditions when it 'happens'. There may be some circumstances when you may need to code that way. Coding doesn't just 'happen'. I've never seen such a circumstance in 15 years of Java but I'm not prepared to rule it out.
                  • 6. Re: Garbage collection
                    SRAVZ
                    So is it a good habit to make all objects and variables null at the end of a method?

                    Or will it be taken care as they would go out of scope?

                    Thanks
                    Sravz
                    • 7. Re: Garbage collection
                      jtahlborn
                      user11138361 wrote:
                      So is it a good habit to make all objects and variables null at the end of a method?

                      Or will it be taken care as they would go out of scope?
                      no, that's a bad habit. it will be taken care of when they go out of scope. the only time you would want to do that explicitly is if you want an object to be eligible for GC while the variable which referred to it is still in scope.

                      e.g.:
                      public void doSomeWork() {
                        Map map = ...; // init big map
                      
                        // do some work with "map"...
                      
                        map = null; // discard map as it is no longer needed (maybe you transformed it into something else)
                      
                        // do some other work which does not require the instance referred to by "map"...
                      }
                      • 8. Re: Garbage collection
                        EJP
                        So is it a good habit to make all objects and variables null at the end of a method?
                        Usually not.

                        I said that above in the first reply.
                        Or will it be taken care as they would go out of scope?
                        I answered that too, in the first reply, and expanded on it in another.

                        Why are you going around in circles?
                        • 9. Re: Garbage collection
                          jwenting
                          EJP wrote:
                          I said that above in the first reply.
                          Or will it be taken care as they would go out of scope?
                          I answered that too, in the first reply, and expanded on it in another.

                          Why are you going around in circles?
                          for some people is usually, eventually, leads to the desired result ;)