Cako
Cako

Reputation: 426

Java and calling garbage collector

I am making some applications (servers and clients).I have a point where I need to call System.gc();

But I found here Why is it bad practice to call System.gc()? that isn't recommended to call gc.

If I use System.gc() the programs runs at ~80MB memory,but without gc,the memory grows up to ~600-700MB and I need to run it on Android phones

Are there other methods to clear memory?

Thanks

EDIT: Seeing the comments,in Android as I have tested (ported 1 hour ago),with System.gc() runs good,I haven't tested without it

EDIT 2:Here are two photos of the programs running in desktop after 5 minutes:

With System.gc():https://i.sstatic.net/um6Ku.jpg Without System.gc():https://i.sstatic.net/SluMF.png

EDIT 3: WOW!! 1 before posted this one application is using about 2GB of RAM!

Upvotes: 0

Views: 126

Answers (1)

Andrew Aylett
Andrew Aylett

Reputation: 40710

It's bad practise because normally the runtime knows more about the state of the system than you do. If RAM is available, why not use it? If RAM isn't available, the system will GC earlier anyway.

In any case, calling System.gc() is merely a hint, and I suspect a hint that's only going to get more likely to be ignored as time passes.

Upvotes: 2

Related Questions