Question: Is it possible to force garbage collection in Java, even if it is tricky to do? I know about `System.gc();` and `Runtime.gc();` but they only suggest to do GC. How can I force GC?

Answer: 
How to Force Java GC
--------------------

Okay, here are a few different ways to *force* Java GC.

1. Click JConsole's `Perform GC` button
2. Use JMap's `jmap -histo:live 7544` command where 7544 is the pid
3. Call the Java Diagnostic Console's `jcmd 7544 GC.run` command
4. Call `System.gc();` in your code
5. Call `Runtime.getRuntime().gc()`; in your code

[![enter image description here](https://i.stack.imgur.com/rJWUT.png)](https://i.stack.imgur.com/rJWUT.png)

None of these work
------------------

Here's the dirty little secret. None of these are guaranteed to work. You really can't [force Java GC](https://www.theserverside.com/video/5-ways-to-force-Java-garbage-collection).

The Java garbage collection algos are non-deterministic, and while all of these methods can motivate the JVM to do GC, you can't actually force it. If the JVM has too much going on and a stop-the-world operation is not possible, these commands will either error out, or they will run but GC won't actually happen.

```
if (input.equalsIgnoreCase("gc")) {
    System.gc();
    result = "Just some GC.";
}

if (input.equalsIgnoreCase("runtime")) {
    Runtime.getRuntime().gc();
    result = "Just some more GC.";
}

```

Fix the darn problem
--------------------

If you've got a memory leak or object allocation problem, then fix it. Sitting around with your finger on Java Mission Control's `Force Java GC` button only kicks the can down the road. Profile your app with [Java Flight Recorder](http://missioncontrol.mcnz.com/allposts.html), view the results in VisualVM or JMC, and fix the problem. Trying to [force Java GC](https://youtu.be/onjlJBDdeTk) is a fools game.

[![enter image description here](https://i.stack.imgur.com/K3sbr.png)](https://i.stack.imgur.com/K3sbr.png)
> 
> Really, I don't get you. But to be
>  clear about "Infinite Object Creation"
>  I meant that there is some piece of
>  code at my big system do creation of
>  objects whom handles and alive in
>  memory, I could not get this piece of
>  code actually, just gesture!!
> 
> 
> 

This is correct, only gesture. You have pretty much the standard answers already given by several posters. Let's take this one by one:

1. I could not get this piece of code
actually

Correct, there is no actual jvm - such is only a specification, a bunch of computer science describing a desired behaviour ... I recently dug into initializing Java objects from native code. To get what you want, the only way is to do what is called aggressive nulling. The mistakes if done wrong are so bad doing that we have to limit ourselves to the original scope of the question: 

2. some piece of code at my big system
do creation of objects

Most of the posters here will assume you are saying you are working to an interface, if such we would have to see if you are being handed the entire object or one item at a time.

If you no longer need an object, you can assign null to the object but if you get it wrong there is a null pointer exception generated. I bet you can achieve better work if you use NIO

Any time you or I or anyone else gets: "*Please I need that horribly.*" it is almost universal precursor to near total destruction of what you are trying to work on .... write us a small sample code, sanitizing from it any actual code used and show us your question.

Do not get frustrated. Often what this resolves to is your dba is using a package bought somewhere and the original design is not tweaked for massive data structures. 

That is very common.