2

There is gc() method both in System & Runtime classes, and System class's gc() method calls that of Runtime.

public static void gc() {
        Runtime.getRuntime().gc();
    }

As per Java code documentation for gc() in Runtime class

Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.

Is there anything special that happens when Runtime.getRuntime().gc() is called? What will happen if I call it and do not call it?

What I already know?

Calling gc() do not invoke Garbage Collector instantly and its upto jvm when Garbage Collection should happen.

EDIT

I want to understand why gc() is still there in Java8. Discussions on other thread do not have accepted answer and are very old.

Amit Kumar
  • 2,355
  • 1
  • 28
  • 64
  • 1
    I have edited my question. @Suvitruf Thnx for advice. Can you tell what difference it will make if I use it? – Amit Kumar Sep 07 '15 at 13:42
  • i would suggest that anybody who would like to make a point on this question should rather post to the thread marked a already having an answer: http://stackoverflow.com/questions/66540/when-does-system-gc-do-anything – jopasserat Sep 07 '15 at 14:19
  • 1
    They are completely different questions, **not duplicates**, that posts asks for the gc() generally, this post asks what changed in that in java8. – peterh Sep 07 '15 at 14:33
  • 3
    Well, then what's the reason to think something changed in java8? – the8472 Sep 07 '15 at 15:08
  • @the8472, nothing would have changed (I am not sure) but is gc() of any use. I know a few disadvantages though. Is there any real benefit provided by calling this method in any scenario? If none. Why Java has given a useless method? – Amit Kumar Sep 07 '15 at 15:15
  • Occasionally there are reasons to call system.gc(), but they're not specific to java 8. If you're asking what changed... then don't you already know why it was used in the past? If not, maybe you should have gathered that information first? – the8472 Sep 07 '15 at 15:25
  • What are those reasons? Can you pls give me some link/reference? – Amit Kumar Sep 07 '15 at 15:26
  • I suppose, there are thousands of other JRE methods you will never use as well. What’s the point of asking for examples about one of these methods you don’t know a use case for? Is this somehow solving any of your problems? – Holger Sep 07 '15 at 15:31
  • Guys I am no java expert as you all are, I was being curious to know (don't have real problem right now) why Java will provide a method that eventually do not have use case for. @Holger, Can you pls give me a link/reference for the JRE methods that don't have use cases? Thanks :) – Amit Kumar Sep 07 '15 at 15:41
  • 1
    I wasn’t talking about methods having *no* use case but about methods *you* will not use. A library as big as the JRE has lots of special purpose methods, which lots of developers won’t find a use case for. That doesn’t imply that there is no use case at all. Nevertheless, I’m guessing that you won’t have a use for, e.g. `Runtime.halt(int)` – Holger Sep 08 '15 at 08:32
  • In my opinion if a method does something, you can find good use of it. Runtime.halt(int) can be used where you want to stop further processing like in case of excessive heating, Security breach detection or in case of anomoly detection(bots hurting human beings :O ). – Amit Kumar Sep 08 '15 at 10:16

2 Answers2

2

Maybe you are writing a benchmarking library and it is important for you to minimise interference from GC while running your benchmarks.

So you may want to call gc() between runs (because you are writing low level stuff and you know that in practice the JRE(s) you target will actually run the GC on calling gc())...

assylias
  • 297,541
  • 71
  • 621
  • 741
  • Thanks for the links, one more question, I know GC can't be disabled but why its written "WARNING: System.gc() was invoked but couldn't detect a GC occuring, is System.gc() disabled?" at http://hg.openjdk.java.net/code-tools/jmh/file/0879b862a1a3/jmh-core/src/main/java/org/openjdk/jmh/runner/BaseRunner.java#l249 – Amit Kumar Sep 07 '15 at 16:42
  • I am accepting the answer as it answers the question to this thread for now. I will try to implement your use case and update in near future. – Amit Kumar Sep 07 '15 at 18:09
  • 1
    @amit_yo, [-XX:+DisableExplicitGC](http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#BehavioralOptions). – Tagir Valeev Sep 08 '15 at 04:23
0

There are several answers related to this topic and lots of discussion over internet. Here are few links that may help the future readers to grasp it better.

  1. Read what oracle has to say about System.gc() in faqs.
  2. A bug has been logged and its still unresolved regarding misleading documentation for System.gc().
  3. https://community.oracle.com/thread/1542855
Amit Kumar
  • 2,355
  • 1
  • 28
  • 64