Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

need clarification on garbage collection

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)How can you force garbage collection?
[a] Garbage collection cannot be forced
[b] Call System.gc().
[c] Call Runtime.gc().
[d] Set all references to null.

The answe i feel is (b) call System.gc()
But the answer shown in someother site is (a) garbage collection cannot be forced

2). From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
[a] 1
[b] 2
[c] 3
[d] 0
plz let me know the answer for this...
help is appreciated
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch.
The answer to the first question is indeed, a. System.gc() is just a suggestion to the Java Virtual Machine, which can be ignored.
The answer to the second question is 0. The only string objects here are String literals, and they are stored in an internal table, and are not GC'd.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I could try to clarify to make sure I understand... when an object has been released ( either set to null or GC suggested, whatever ), Java will/can clean this stuff up on it's next pass. All you can do is mark stuff for collection - right? There is no way to force it whatsoever?
 
author
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry David buts that's what the JVM spec says. You can set a reference to null, and the object is eligible for garbage collection, but you cannot force it. There are books you can read and probably online refences on Sun's web site.
The implementation is generally a mark and sweep implementation on a low priority thread, and if you code simple experiments, you can see garbage collection in action, by overriding the finalize method and then callng System.gc after setting references on instances to null.
Regards,
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic