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




<johndesp@xxxxxxxxx> 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


.



Relevant Pages

  • Re: forcing finalize()
    ... I thought that object is gc'ed when there are no references left. ... There are some special handling for string literals where Java ... sufficiently strong referencesis eligible for garbage ... // Loop B ...
    (comp.lang.java.programmer)
  • Re: Is garbage collection here yet?
    ... For instance, if you have string ... garbage is, what the programm cannot access anymore. ... References are "not Tcl" in about the same way as dynamic code ...
    (comp.lang.tcl)
  • Re: Getting file name from the file path error
    ... I checked one users tools - References and it appears that it ... It is part of a code module in an Excel ... end of statement with the As highlighted after the(stFullName As String). ... Sub GetFileNameAs String ...
    (microsoft.public.excel.programming)
  • Re: accessors
    ... mentioned that returning references from functions makes using ... boundaries, but if i did, "string const& nameconst" is functionally ... the same as "string nameconst", so i don't see why i'd have to change ... anything (but if i did, the change does not really affect client code, ...
    (comp.lang.cpp)
  • Re: Importing Excel
    ... Function LastInStr(strSearched As String, strSought As String) As Integer ... Didn't realize you haven't set references yet. ... > named Microsoft Excel and select the box. ...
    (comp.databases.ms-access)