This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Grabage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Grabage Collection" Watch "Grabage Collection" New topic
Author

Grabage Collection

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,

Can you pl spare a moment and let me know :-

1. Is every new call will make a new object?
2. or there will be only location reference value change?
3. WHat will happene if GC gets called?( Just a assumption that it gets activcated after first time we make the tt null)

regards,
Arun

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
In your example, three separate objects are created. After the third object is created, the first two would be eligible for garbage collection.

The Garbage Collector in your jvm works on its own cycle, periodically looking for eligible objects. If your program runs short of storage, the Garbage Collector will make an extra pass to try to save you from program termination. System.gc() simply asks the Garbage Collector to make a collection pass. The jvm can either do this or do nothing. The Sun doc suggests that Sun's jvm's will usually act on your request. Sun even suggests that you can run System.gc() repeatedly until no more memory is recovered.

The main reason for you to call System.gc() is to prevent garbage collection, which takes a certain amount of cpu time, from happening just when a time-critical piece of code is running. However, the execution delay from a garbage collection pass on a modern CPU is very small. System.gc() is also the basis for many exam questions, so it's good to know it even if you never need it in your work.
[ October 23, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817



Just to be explicit, the object created on line 4 is eligible for garbage collection after line 8 has finished. The object created on line 8 is eligible for garbage collection after line 11 has finished.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
 
subject: Grabage Collection
 
Similar Threads
garbage collection and finalize method- inconsistent result
Question about finalize method of GC
Garbage collection output doubt
Doubt with notify/ notifyAll method
Garbage collection problem