Stupid Question - losing my mind - Garbage Collection and methods..

Discussion in 'Java' started by johndesp@gmail.com, Dec 26, 2005.

  1. Guest

    Guys, I have been working through a memory leak and need to go back to
    square one for some understanding about have Garbage collection works
    with java 1.4.x.

    Will Garbage Collection clean up the String variable named test in the
    method doSomething(String inme) ? I ask this stupid question because I
    am seeing odd results. Also, Can you explain why? Thanks


    Example:


    inside...some servlet code...

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    doSomething("whatever");


    }


    private boolean doSomething(String inme) {

    String test = "";

    test = inme;

    boolean sendback = true;


    return sendback;

    }
     
    , Dec 26, 2005
    #1
    1. Advertising

  2. Adam Maass Guest

    <> wrote:
    > Guys, I have been working through a memory leak and need to go back to
    > square one for some understanding about have Garbage collection works
    > with java 1.4.x.
    >
    > Will Garbage Collection clean up the String variable named test in the
    > method doSomething(String inme) ? I ask this stupid question because I
    > am seeing odd results. Also, Can you explain why? Thanks
    >
    >
    > Example:
    >
    >
    > inside...some servlet code...
    >
    > public void doGet(HttpServletRequest req, HttpServletResponse resp)
    > throws ServletException, IOException {
    >
    > doSomething("whatever");
    >
    >
    > }
    >
    >
    > private boolean doSomething(String inme) {
    >
    > String test = "";
    >
    > test = inme;
    >
    > boolean sendback = true;
    >
    >
    > return sendback;
    >
    > }


    Garbage collect collects objects that have no references to them.
    (Exception: softly or weakly reachable objects might be collected -- but
    that's another discussion.) Your method doSomething has two reference
    variables: inme and test. inme is a reference to the actual parameter, the
    string value "whatever". test initially starts as a reference to the string
    value "", but is then re-assigned to the value of the variable inme. At this
    point, the string "whatever" has two references to it, and the string value
    "" has 0. "" might be eligible for garbage collection. When doSomething
    returns, all of its references are cleared from the stack, and the string
    value "" and the string value "whatever" are both eligible for garbage
    collection. (That is, if they weren't string constants -- string constants
    are special cases when it comes to garbage collection -- since the VM
    guarantees that all string constants with the same value are actually
    references to the same String object, they are held on to by the system for
    far longer than other objects.)

    What odd results are you seeing?

    -- Adam Maass
     
    Adam Maass, Dec 26, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On 26 Dec 2005 06:34:16 -0800, ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    >doSomething("whatever");
    >
    >
    >}
    >
    >
    >private boolean doSomething(String inme) {
    >
    >String test = "";
    >
    >test = inme;
    >
    >boolean sendback = true;
    >
    >
    >return sendback;


    there are no strings to clean up. There are two string literals
    "whatever" and "". They will be needed next time your method is run,
    so they will not be GCed. Don't confuse references with String
    objects.

    Let us say instead you had written

    doSomething( Double.toString(Math.sqrt( 50.0 )) );

    private boolean doSomething(String inme) {

    String test = "";

    test = inme;

    boolean sendback = true;

    return sendback;


    Now when you return, the test reference to the string will go out of
    scope. Then the temporary will be no longer needed. At that point the
    string will be eligible for GC.


    I would also write your code in a more abbreviated form. You have two
    pointless assignments:

    String test = inme;
    return true;
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 26, 2005
    #3
  4. Roedy Green Guest

    Roedy Green, Dec 26, 2005
    #4
  5. Guest

    Thanks Guys.. I am cleaning up my code and removing pointless
    assignments. If there is a need in a method to instantiate a String,
    will it eventually get cleaned up by the Garbage Collector?

    thanks
     
    , Dec 26, 2005
    #5
  6. Guest

    > If there is a need in a method to instantiate a String,
    > will it eventually get cleaned up by the Garbage Collector?


    It will get cleaned up sometime between the time when you finish using
    it and the time when your program ends.

    The important thing to know is that it will NOT be cleaned up before
    you have finished using it, and it will NORMALLY be cleaned up before
    you run out of memory.

    If you are worried about memory usage, you can see it at runtime quite
    well (I believe), using the Java Memory Profiler:
    http://www.khelekore.org/jmp/

    Otherwise, just don't worry about it. That's what garbage collection
    is there for, so that you don't have to worry about freeing up memory.
     
    , Dec 26, 2005
    #6
  7. IchBin Guest

    wrote:

    > If you are worried about memory usage, you can see it at runtime quite
    > well (I believe), using the Java Memory Profiler:
    > http://www.khelekore.org/jmp/


    You can also use jconsole.exe that resides locally at JAVA_HOME\bin\

    'Using JConsole to Monitor Applications'
    http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
     
    IchBin, Dec 26, 2005
    #7
  8. Roedy Green Guest

    On 26 Dec 2005 09:22:48 -0800, ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    > If there is a need in a method to instantiate a String,
    >will it eventually get cleaned up by the Garbage Collector?


    All objects, including strings will be gced once nothing points to
    them. You rarely instantiate a String with new. You normally create
    one via concatenation (which is really just a StringBuilder.toString).
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 26, 2005
    #8
    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. Normie Smith
    Replies:
    0
    Views:
    382
    Normie Smith
    Apr 5, 2004
  2. Brandon McCombs
    Replies:
    4
    Views:
    528
    Richard Wheeldon
    Aug 28, 2006
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    1,011
    Øyvind Isaksen
    May 18, 2007
  4. Greg C.
    Replies:
    8
    Views:
    418
  5. Normie Smith

    HELP - Link Button on User Control 'Losing its Mind'

    Normie Smith, Apr 5, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    129
    Normie Smith
    Apr 5, 2004
Loading...

Share This Page