Forum Xamarin.Forms

Android Foreground Service Preventing Memory Release?

ericcericc Member ✭✭
edited November 2019 in Xamarin.Forms

I created an android application with a foreground service that is always running. Initially, when the application is completely shut down (both process and service is not running), I try to use a broadcast to wake up the service, then my application take up 36MB of memory.
Next, I open the application and the memory usage grow up to 121MB. Finally, I tried to swipe out to kill the app, OnDestroy() method in MainActivity get called and I manually called GC.Collect() within this method, however, it's still take up 109MB of memory

My foreground service is just listening to step detector sensor and store the result to 'SharedPreferences' so I believe it won't take over hundred MB. Is there anyway to release more memory when the app is swiped out by user? Thank you.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can try to use Handler.RemoveCallbacksAndMessages(Object) Method in your onDestroy method.

     if (handler != null)
     handler.RemoveCallbacksAndMessages(runnable);
    

    I found a similar thread, he had two handlers, after removing second hander, this issue was disappear.
    https://stackoverflow.com/questions/56072256/android-foreground-service-keep-data-in-memory

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @ericc Are there any update for this issue, If the reply is helpful, please mark it as anwer, it will help others who have similar issue.

  • ericcericc Member ✭✭

    @LeonLu said:
    You can try to use Handler.RemoveCallbacksAndMessages(Object) Method in your onDestroy method.

     if (handler != null)
     handler.RemoveCallbacksAndMessages(runnable);
    

    I found a similar thread, he had two handlers, after removing second hander, this issue was disappear.

    Sorry for the late reply. I have seen this post before but it is not my case. There is no handler in my service. My service will only listen to step detector and write it to local store.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Can you share a demo that you reproduce this issue ?

Sign In or Register to comment.