The Artima Developer Community
Sponsored Link

Java Answers Forum
Garbage collector question

7 replies on 1 page. Most recent reply: Dec 4, 2003 6:12 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
correro

Posts: 1
Nickname: correro
Registered: Apr, 2003

Garbage collector question Posted: Apr 15, 2003 8:07 PM
Reply to this message Reply
Advertisement
I'm reading Thinking in Java by Bruce Eckel. He says that Java's garbage collector automatically frees memory used by objects created with "new".

My question is, if my String object is created without using "new", (e.g., String s = "My String here"), will the garbage collector free it like it does a string created with "new"?

Thanks for your answer!


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Garbage collector question Posted: Apr 15, 2003 9:00 PM
Reply to this message Reply
"He says that Java's garbage collector automatically frees memory used by objects created with "new"."

Just stick to the words you have read. Since you are not using new keyword in :

String str = "you string gies here" ;

garbage collector won't do anything with it. For futher understanding of please read the topic - "String Pool", which I believe, is not described in detail in Thinking in Java.

varanasi R eswar

Posts: 3
Nickname: eswar
Registered: Nov, 2003

Re: Garbage collector question Posted: Nov 30, 2003 6:27 AM
Reply to this message Reply
hi kishori,
im Eswar , B.tech , final year, to my knowledge , java frees up whenever it gets out of any block of code , thats what after a function was left its control all its local data are freed up , that it what i mean .

i have a problem , with buttons , plz help me ,
if 1 2 3 is the form of buttons and labels
4 5 6
7 8 *
for a game design , how should i know if i click 6 ,
it is adjacent to * the puzzle game :
the result must be
1 2 3
4 5 *
7 8 6
* == " " puzzle game , can u help me
what is behind to get the adjacent buttons list ,if i press one (click)


bye
Eswar

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Garbage collector question Posted: Nov 30, 2003 6:46 AM
Reply to this message Reply
Varanasi, this doesn't seem to have anything to do with the question originally posted. I suggest you post this as a new question in a new thread.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Garbage collector question Posted: Nov 30, 2003 2:45 PM
Reply to this message Reply
String myName = "God";
myName = "Test";


Don't you guys think after the second assignment God is elegible for Garbage Collection? :-)

Graham Lea

Posts: 3
Nickname: grlea
Registered: Nov, 2003

Re: Garbage collector question Posted: Nov 30, 2003 6:37 PM
Reply to this message Reply
I believe String literals are not allocated in the heap (garbage collection only works over the heap, I think) but in a "constant pool" - which is part of a class file - and as such are never removed from memory.

See:
http://java.sun.com/docs/books/vmspec/html/ConstantPool.doc.html#38759
http://java.sun.com/docs/books/vmspec/html/Overview.doc.html#22972
http://java.sun.com/docs/books/vmspec/html/VMSpecTOC.doc.html

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Garbage collector question Posted: Dec 2, 2003 12:43 PM
Reply to this message Reply
Yes, the links talk about constant pool and I knew that before. But Strings never get Garbage collected???? ThatÂ’s what puzzling me

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Garbage collector question Posted: Dec 4, 2003 6:12 AM
Reply to this message Reply
I hardly believe it too, at least if I'm using concat operations to create a String.

I allways thought the compiler would replace
s = "x" + "y";
with
a = new String ("x".concat("y);

So, the call of
String s;
while (true)
{
s="x" + "y";
System.gc();
}
would result in a "out of memory" exception or cause problems with heap?

Flat View: This topic has 7 replies on 1 page
Topic: what actually  java bytecode Previous Topic   Next Topic Topic: java.lang.VerifyError: (class:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use