Suvasis
Suvasis

Reputation: 1481

How to make sure String objects are garbage collected in Java

Here is the code snippet.

method(){
    String s1="abc";
    String s2 = new String ("abc");
    s1=null;
    s2=null;
    --------
    ---------
     }

At the end is s1 & s2 objects are exists? How you will make sure these objects are garbage collected ?

Upvotes: 0

Views: 5187

Answers (5)

KarthikRaja
KarthikRaja

Reputation: 66

String s2 = new String ("abc"); Here 'abc' will be created in regular, garbage collectible heap area. So as soon as you make S2 null, this string object is eligible for garbage collection. This is assuming that your programm do not have any other reference to this particular string object "abc".

String s1="abc"; In this case, "abc" will be created in special area of heap called literal pool or string pool. Making "abc" null does not make "abc" eligible for garbage collection since JVM will try to reuse this "abc" in future. Baseline in this case is, normal garbage collection rules won't apply here.

Hope this helped. :-)

Upvotes: 0

Sudhir Dhumal
Sudhir Dhumal

Reputation: 970

Garbage collector runs periodically(time period is JVM dependent). Java maintains table of objects and its references when reference is broken (probably by assigning null to reference) then on next execution of GC (garbage collector) object's having no reference will be deleted (If something goes wrong with GC then object will not garbage collected - very very rare condition), which is totally dependent on JVM. You can send request to JVM to run GC by using following code (Processing your request is once again JVM dependent):

Runtime.getRuntime().gc();

or

System.gc();

Programmer don't have to worry about the running GC mostly JVM will handle execution of GC. There are lots of enhancements made to the garbage collectors. Java (latest version) comes with G1(Garbage First) collector which is a server-style garbage collector which runs more effectively. G1 is a great replacement for CMS (Concurrent Mark-Sweep Collector). If you want to know more about garbage collector then you should read the pages:

[http://docs.oracle.com/javase/7/docs/technotes/guides/vm/gc-ergonomics.html][1] [http://docs.oracle.com/javase/7/docs/technotes/guides/vm/cms-6.html][2] [http://docs.oracle.com/javase/7/docs/technotes/guides/vm/par-compaction-6.html][3]

Upvotes: 0

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29233

If the question is how to make sure, the answer is fairly simple. You can never make sure that any object will be garbage collected. Read this to understand what garbage collection really is and how to reason about it.

If the question is how to hint for a collection, then set all the references of unwanted objects to null and call System.gc(), which will request (not force) a collection. Nothing is guaranteed to be released using this method, but often it's the closest thing you can get.

If you want to do this specifically for strings, because they may contain sensitive data or something along these lines, use a char[] to store that data instead of a String, because you can change the primitive values of the array at will and erase them when you're done.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49432

Objects referenced to by s1 and s2 are eligible for garbage collection once s1=null and s2=null provided that no other references to that Object exists or when the method exits, provided that the Objects were only referenced by the local variables.An object once created uses some memory and the memory remains allocated till there are references for the use of the object.When there are no references for an object, it is assumed to be no longer needed and the memory occupied by the object *can be reclaimed.*An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static refrences in other words you can say that an object becomes eligible for garbage collection if its all references are null.

There are methods like System.gc() and Runtime.gc() which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen. Java programmers can not force Garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection. Forced GC is sign of bad coding.Once should in turn always look to minimize creation of unnecessary objects and references to those objects.

Upvotes: 1

They get garbage Collected after they go out of scope.

Unless you're actually having serious performance issues, I'd stop worrying about it so much and let the garbage collector do it's thing.


You should be careful though, there are some kinds of elements such as file streams, open sockets, and such that are not managed like that. you have to close those.

Upvotes: 0

Related Questions