• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, there
as far as i know, if we creat object throught new, the automatic garbage collector will take care of cleaning up. How about java primitive value?
i came cross that finalize() will take care of them. I also know that before garbage collection take place, the finalize() always will be called. i am wondering who on earth will detetroy the object created without using new.
In the following code. will the object referenced by s garbage collected after setting it as null.( someone claimed that string literal will not be GC, is he or she right?)
String s="garbage collector"
s=null
please help!
thanks in adv....

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You don't have to worry about g.c. and primitive types. G.C. frees the memory ocupied by objects. If the object hold primitive variables they were placed in the image of the object in the heap. So they will be destroyied at the same time that the object. If the fields are static they are stored in the method area along the rest of the data for its class. If the class is ever unloaded so they are. Variables declared in a method are destroyied when the methods completes.
This code shows that String literals are not made eligable for g.c.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are a special case of objects. Strings may be created in two ways: either with the keyword new in which case they will be garbage collected or with a String literal like it is the case here and they will not be garbage collected.
This topic has been discussed an awful lots of times, please, make a search in this forum and look at what you get. I think it is useless to always and always repeat the same things again and again and again !!! Thank you !
The search engine works well and is very efficient
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are primitive types garbage collected? Well, yes and no. Let's take an "int" and see what happens.
If our "int" is a member variable of a class, it's easy. An instance of the class will ultimately be garbage collected, and the "int" will go with it.
But what about local variables? Eg,These are not garbage collected. Instead, they are allocated on the stack. When the thread enters the method, the JVM allocates a stack frame with enough space for all local variables and some administrative stuff besides. This stack frame is deallocated the moment you leave the method (whether it's with a return, by throwing an exception or by encountering the closing brace). Memory management is directly tied to the foo() method block and completely automatic (they are called automatic variables in C/C++ parlance).
What about an object, then?You might wonder, is this automatically allocated and deallocated too? Well, no. There are actually two allocations here. First, a reference variable holding a reference to a Bar object is allocated on the stack. It will be deallocated along with the rest of the stack frame as soon as you leave the foo() method. Second, a Bar object which is allocated on the heap. A reference to this object is assigned to the "bar" variable. The object will ultimately be garbage collected.
Then your final question.String literals take a rather privileged position in Java. There is only ever one copy of each literal:These literals are not subject to garbage collection. They are part of a "string pool" that stays around as long as your application lives. You add new strings to the pool, or retrieve existing instances, using the intern() method:Hope this helps
- Peter
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A post by a bartender somewhere in the nether regions of javaranch also postulated:


"good" is a literal and can never be collected, but "goodmorning" can be collected.
(s += "morning" is equivalent to s = s + "morning", and since s isn't a constant, the expression s can be garbage collected.


I have question regarding the first line of the code sample above. e.g. String s = "foo";
Is anything created on the heap in this statement ?
Pho
[This message has been edited by Pho Tek (edited November 25, 2001).]
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, nothing is created on the heap. A reference to the string constant "good" is assigned to the local variable "s".
- Peter
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Peter


No, nothing is created on the heap. A reference to the string constant "good" is assigned to the local variable "s".


But how is the reference assigned to a variable?
I have tried several examples and all of them decompiles in a bytecode of type "ldc # pool-entry-index".
According to Bill Venners' Inside The Java 2 Virtual Machine page 618 describing this bytecode:


Otherwise the entry is a CONSTANT_String_info entry, and the virtual machine pushes a reference to the interned String object that was produced by the process of resolving the entry onto the operand stack.


Thus I would say that the mere String s = "Hola"; produces an entry in the constant pool that gets resolved to the addres of the proper object. This is so because the addres must be assigned to a variable. In other examples the string literal could be used as a parameter to a method. Then the ldc bytecode pushes the reference
to the interned String object onto the operand stack just before calling the method.
What dou you think?
 
reply
    Bookmark Topic Watch Topic
  • New Topic