haii..

do anyone know any garbage collection command for java....
did we have to did that command for all object or will be done collectively??

thank u.....

Recommended Answers

All 5 Replies

I'm pretty sure it's something along the lines of--

Runtime.getRuntime().gc();

Or the long way--

Runtime rt = Runtime.getRuntime();

rt.gc();

I do believe you can do the same using System instead of Runtime (without calling getRuntime - just System.gc() )


This simply suggests for the Garbage-Collecting threads to begin garbage collecting. It does not mean that it will be immediately done. In that respect, it's not really known when garbage collecting will be done. Lastly, explicitly calling the gc causes additional overhead since the call to gc is suggesting that there are unreferenced objects to collect and the threads are signalled to do extra work and search through all possible objects to garbage collect ones that are reachable and unreferenced.

The process is slightly more complicated than mentioned. This is a watered down and brief explanation.

The garbage collector is very smart. There's hardly ever a need to use the provided commands to SUGGEST it runs.
If you think you need to, you almost certainly have an error somewhere in your code causing excessive memory use that prevents garbage collection by holding on to objects unnecessarilly.

commented: After learning the hard way in the past, I completely agree. +3

The garbage collector is very smart. There's hardly ever a need to use the provided commands to SUGGEST it runs.
If you think you need to, you almost certainly have an error somewhere in your code causing excessive memory use that prevents garbage collection by holding on to objects unnecessarilly.

problem solved using System.gc;

Thank you all

like I said, it's not solved. It's masked.

Hey friend in java we cannot force the garbage colllector but can request it using
System.gc();
command

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.