0

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.stack.imgur.com/um6Ku.jpg Without System.gc():https://i.stack.imgur.com/SluMF.png

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

Community
  • 1
  • 1
Cako
  • 396
  • 3
  • 15
  • The GC will reclaim objects *when* it can (and *when* it thinks it needs to) - for systems like Android the trick is to design the application such that not not so much needs to be loaded at a time. This is why the Views have self-contained lifetimes in Android, etc: the design is to *limit* the working footprint. – user2864740 Dec 20 '13 at 20:38
  • To clarify: Without calling `System.gc()` the memory grows up to 600, 700 MB *when run on a mobile device*? Or are you doing these tests on a desktop machine with a desktop JRE? –  Dec 20 '13 at 20:38
  • Are you sure that the app won't run on Android phones? It's possible it's keeping 600mb of data in memory (though it seems high) because that's what you've allocated, and if less were available a gc() might be triggered automatically. – Matt Dec 20 '13 at 20:38
  • @delnan no,I have tested the application without System.gc in Desktop,Windows 7 x64 with 8GB memory – Cako Dec 20 '13 at 20:39
  • one rare case where calling gc can be useful is the point in your program where you know you don't need responsiveness for a few hundred millis, and where you know you will need a lot of memory in a close future. – njzk2 Dec 20 '13 at 20:40
  • 1
    Well there's your problem. The JVM on your desktop machine probably doesn't even share much code with the Dalvik VM on Android devices, and regardless of their lineage any sane GC will adjust their behavior (how often to collect etc.) to the amount of available memory. I predict you won't see nearly as much memory consumption on mobile devices. –  Dec 20 '13 at 20:41
  • @delnan I need the less memory,because I need to run these programs on a Raspberry Pi – Cako Dec 20 '13 at 21:01
  • @Cako To repeat that point, I predict that when you run with less memory (on an Android device, on a Pi, or anywhere else really) the GC will keep the memory consumption lower to make it fit. –  Dec 20 '13 at 21:06

1 Answers1

2

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.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95