Re: Stupid Question - losing my mind - Garbage Collection and methods..
- From: "Adam Maass" <adam.nospam.maass@xxxxxxxxxxx>
- Date: Mon, 26 Dec 2005 08:41:55 -0800
<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
.
- References:
- Stupid Question - losing my mind - Garbage Collection and methods..
- From: johndesp@xxxxxxxxx
- Stupid Question - losing my mind - Garbage Collection and methods..
- Prev by Date: Re: Java Datagram Question
- Next by Date: Re: Java Datagram Question
- Previous by thread: Stupid Question - losing my mind - Garbage Collection and methods..
- Next by thread: Re: Stupid Question - losing my mind - Garbage Collection and methods..
- Index(es):
Relevant Pages
|