1

Confused as how Garbage Collection works in below cases. Considering the below piece of code.

String s1 = "abc"; // s1 points to "abc"
String s2 = s1;   // s2 points to "abc"
String s3 = "abc1"; // s3 points to "abc1"

s1 = s3; // s1 points to "abc1"
s2 = null;  // s2 reference is removed, "abc" is no longer referenced now

After this, will "abc" be eligible for GC.

Also if same above example, if I use new String()

String s1 = new String("abc");

Now what will be the result.

Also are there any tools, through we can monitor Garbage Collection, as which objects are getting collected by GC

5 Answers 5

3

In first case

 String s1 = "abc"

your string will go in string pool maintained by JVM and will be never garbage collected.

However in second case

 String s1 = new String("abc");

general rules apply, and your string object will get garbage collected as soon as its scope is ended.

Sign up to request clarification or add additional context in comments.

9 Comments

strings in the string pool may be garbage collected.
assylias just saved me some typing. If a String instance corresponding to a string literal becomes uncreachable, it is subject to garbage collection on newer HotSpot versions. A stronger point is that none of this is by specification, therefore any statements to that effect must be carefully qualified with the exact platform, OS, and Java runtime version number. But then their relevance becomes quite deflated.
@MarkoTopolnik - String constants can be GCed from Permgen space as well or only after they were moved to the other side of the heap in java 7?
@assylias - You mean when the ClassLoader of the current class with the String constant is GCed, the String constant is also GCed?
@TheLostMind As far as I remember, String pool was moved from PermGen to regular heap with HotSpot 6. On HotSpot 8 there's no more PermGen, anyway.
|
2

You can use Visual VM, which is provided by Oracle itself to monitor many things including memory.

Another small hack to gain insight.Consider the following piece of code.

String a = "abc"

String b = "abc"

If we do a==b which compares references,it will actually return true since Java knows that abc is same and it will be referenced to the same memory in the String pool.

So when there is no reference to the string,it is a clue to GC to get its job done.

Comments

2

for garbage collection monitoring u can refer this

http://www.cubrid.org/blog/dev-platform/how-to-monitor-java-garbage-collection/

2 Comments

Oops — this page has been removed. :)
:O iam able to access it cubrid.org/blog/dev-platform/…
1

String s1 = "abc"; // s1 points to "abc" Here, "abc" will be added to the String Constants Pool and will usually not be GCed. Any string literal (within double quotes) will usually not be GCed.

String s1 = new String("abc");

The above line creates 2 Strings. "abc" will be added to the String constants pool (assuming it is not present there already) and another String with value "abc" will on the heap. The String object present on the heap can be GCed once it becomes unreachable (i.e, there are no references to it)

1 Comment

s1 = new String("abc") does not give any entry to String pool. "abc" will be in String pool if you call intern() on s1 or you assign "abc" directly to a reference. @TheLostMind
0

Well it's not as simple as it sounds. The answer depends on the version of the Java that you're using. All the string literals that can be resolved at the compile time (like the ones in your example) are coming from String Constant Pool basically a HashTable created at the start of JVM and updated as and when classes are loaded.

If you're working in Java 6, then this String Constant Pool is part of PermGen, a space outside of your heap but managed by the java process. Just like Heap, GC won't be triggered in this region until its full. Unless you're dealing with millions of classes or your PermGen size is very small, this region won't fill up fast and GC won't be so often. So answering to your question, even after removing all references to "abc", it won't be GC'ed until the PermGen fills up.

Starting from Java7, String Constant Pool is also a part of Heap, so your object allocation rates might effect the frequency of GCs and "abc" might be cleared sooner than in Java6.

There're no specific tools as to check which objects are GC'ed but we'have some cool tools that come with Standard JDK which will help us understand GC and memory consumption better and tune it

VisualVM - has a plugin called VisualGC - with this you can understand the trends in GC

FlightRecorder in Java Mission Control shows more data about GC and memory consumption than VisualVM

Have a look at this link in case you need more info :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.