• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the use of System.gc method  RSS feed

 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is a question I was asked in an interview. What is the purpose of calling System.gc method when it cannot force garbage collection to happen? That is, why should we call this method when it doesn't guarantee the garbage collection? What is the advantage we get in calling this method ? Please let me know if you know the answer.

Thanks,
Lavanya
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, what was the answer you gave during the interview?

Henry
 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only knew that it can suggests the garbage collector to run. So I couldn't give a solid convincing answer. Please help.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try a different approach. Do you think your answer was right, or wrong? Explain why you feel that way.
 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever I told was right only. But that was not the answer. Correct ? The question is not "what does System.gc do". Rather the question is what is the purpose of calling the method when it doesn't guarantee GC.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A suggestion of "now would be a good time to run gc" is better than no suggestion.

Personally I have used it right after showing the user a complex screen that I know will take time to read and react to. Much better than having the system go do gc() right when the user is expecting a prompt response.

Bill
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you almost never need to call System.gc();.

Don't add it to your code just because you think it is a good idea. Only optimize when you know that there is a performance or memory usage problem - for example, because you measured performance and memory usage with a profiler, and you discovered that there is a problem. Adding it just because you think it will help is superstitious programming.

The garbage collector is a very sophisticated piece of software, it's very hard to predict what exactly it will do, and trying to "help" it by calling a method like System.gc(); often has no real effect at all, and it can even be counterproductive (you could be unknowingly counteracting the process).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK all Sun implementations do actually run the GarbageCollector when System.gc() is invoked. Example code that demonstrates this:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not really relevant, though, since it's explicitly not guaranteed, and not everybody uses Sun JVMs.

(Nor is that code that demonstrates what you claim it does, right? Only that that particular combination of code, on your machine, with that JVM, using your current configuration and startup options, during those particular runs, etc...)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:AFAIK all Sun implementations do actually run the GarbageCollector when System.gc() is invoked.


Yes, they do... The "gc() is only a suggestion" statement, while correct, is very overblown. The reason it is only a suggestion is because there are cases where the GC can't run, so when requested, will be ignored. The other main reason is that it is possible to turn off explicit GC via a -XX switch.

Henry
 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, there is no advantage of calling System.gc method. Right?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should normally never need to call this - just let the garbage collector do its work, it normally knows best when to cleanup the garbage. Only in very special cases, when you have discovered that there is a real problem, you might try to use it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!