1

I have two activities in my android application. When I switch from first activity to second activity, gc starts and makes second activity to lag until it completes. I decided to make a splash screen (loading screen) that will not close until gc finishes but I do not know how to get gc status pro-grammatically. Is there any class of it? Please let me know how can I get this scenario!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
SWIK
  • 714
  • 7
  • 12
  • please add the code which you are using – Manohar Aug 24 '19 at 08:39
  • Intent intent = new Intent(Graphs.this,Waitsplashscreen.class); startActivity(intent); – SWIK Aug 24 '19 at 08:55
  • I think you're doing some expensive operation on main thread....you can use Android Studio Profiler to find out what actually is blocking your Main thread. https://developer.android.com/studio/profile/android-profiler – m3g4tr0n Aug 24 '19 at 12:47

3 Answers3

4

To begin with, in Android, garbage collection is organized by the ART - Android Runtime or DVM - Dalvik Virtual Machine (on older devices). As ART/Dalvik are essentially specialized versions of JVM, they have similar approach to GC, hence it is solely managed by the system and not by the user.

Hence, you don't get to control the garbage collection in Android.

Indeed, you can call System.gc(), but it's nor guaranteed nor recommended to do. You are expected to completely forget about garbage collection process and leave it to the system.

While you cannot control it, you are still responsible to manage the memory and prevent excessive memory usage as much as possible. A few tips, you should consider:

  • Release bulky objects (remove hard references pointing to them) as soon as you're done working with them;
  • Utilize multithreading to your needs, threads will work in parallel and faster (especially on multi-core processors);
  • Optimize your algorithms, even basic list iterations could potentially slow the process and leak memory if done incorrectly
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • But I do not want to call gc, I want to get the status of it! Such that when it finish I can do what I want – SWIK Aug 24 '19 at 09:17
  • @SWIK - You don't control it, you don't get to know when it's done. You can attempt to use the `finalize()`method, but it's not recommended and not reliable either. Here is an example of how you could possibly do it: https://stackoverflow.com/questions/2298784/determine-when-the-android-gc-runs – Sergey Emeliyanov Aug 24 '19 at 10:06
2

Thank you guys for answers. After some working i found what the problem in code. I was executing this async class in while loop with new instance. So it keeps memory increasing and after two hours it starts hangs or when activity switched gc executes.

SWIK
  • 714
  • 7
  • 12
1

I think the answer by @Serj sums it up quite good. Maybe you find a workaround to get the GC triggered if you keep the instance of your old activity, and thus have it still being referenced, until your splash screen is set up. Then you remove the last references and hope for the GC to be called - but yet it could happen that it will get called later. It's a good question how to see the status of the GC, maybe you can read out the memory and see if its filled or not? The best advice is refactoring and using objects only in scopes in which they are needed.

yesyoor
  • 96
  • 8