Search code examples
androidmultithreadinggarbage-collectionmicrocontroller

Can I run a background thread in Android indefinitely?


I'm building an android application that will run as a standalone app on a microcontroller.

I have to add a background thread in my app that performs some task and is called every 20ms indefinitely. On running my application, I get the following warnings.

I/ample.app: Background concurrent copying GC freed 3461(211KB) AllocSpace objects, 36(1872KB) LOS objects, 49% free, 2MB/4MB, paused 509us total 104.562ms 

I/ample.app: Background concurrent copying GC freed 7921(410KB) AllocSpace objects, 36(1872KB) LOS objects, 49% free, 1933KB/3MB, paused 2.204ms total 109.949ms 

I/ample.app: Background concurrent copying GC freed 2823(175KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 2022KB/3MB, paused 1.047ms total 104.209ms 

I/ample.app: Background concurrent copying GC freed 2758(176KB) AllocSpace objects, 36(1872KB) LOS objects, 50% free, 1918KB/3MB, paused 550us total 101.114ms 

I/ample.app: Background concurrent copying GC freed 2725(177KB) AllocSpace objects, 32(1664KB) LOS objects, 50% free, 1918KB/3MB, paused 503us total 102.727ms 

I/ample.app: Background concurrent copying GC freed 2717(161KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 502us total 114.887ms 

I/ample.app: Background concurrent copying GC freed 2592(144KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 492us total 101.373ms 

I/ample.app: Background concurrent copying GC freed 2724(176KB) AllocSpace objects, 32(1664KB) LOS objects, 50% free, 2022KB/3MB, paused 504us total 106.705ms 

I/ample.app: Background concurrent copying GC freed 2880(176KB) AllocSpace objects, 36(1872KB) LOS objects, 50% free, 1918KB/3MB, paused 501us total 101.745ms 

I/ample.app: Background concurrent copying GC freed 2599(161KB) AllocSpace objects, 32(1664KB) LOS objects, 50% free, 1918KB/3MB, paused 1.229ms total 107.834ms 

I/ample.app: Background concurrent copying GC freed 2855(193KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 503us total 119.165ms 

I/ample.app: Background concurrent copying GC freed 2594(160KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 689us total 117.995ms 

I/ample.app: Background concurrent copying GC freed 2470(176KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 502us total 107.202ms 

I/ample.app: Background concurrent copying GC freed 2744(160KB) AllocSpace objects, 32(1664KB) LOS objects, 50% free, 1918KB/3MB, paused 504us total 114.165ms 

I/ample.app: Background concurrent copying GC freed 3100(177KB) AllocSpace objects, 32(1664KB) LOS objects, 50% free, 1918KB/3MB, paused 507us total 106.392ms 

I/ample.app: Background concurrent copying GC freed 2722(177KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 501us total 112.687ms 

I/ample.app: Background concurrent copying GC freed 2719(177KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 596us total 102.322ms 

I/ample.app: Background concurrent copying GC freed 2594(160KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 586us total 112.978ms 

I/ample.app: Background concurrent copying GC freed 2842(161KB) AllocSpace objects, 32(1664KB) LOS objects, 49% free, 1918KB/3MB, paused 603us total 110.437ms 

D/QueuedWork: waited: [<1: 0, <2: 111, <4: 30, <8: 147, <16: 712, <32: 24, <64: 0, <128: 0, <256: 0, <512: 0, <1024: 0, <2048: 0, <4096: 0, <8192: 0, <16384: 0, >=16384: 0] 

Is it okay to ignore these warnings? Although my app works just fine for now, I'm concerned that at some point this warning may lead my application to crash.

Any help would be appreciated. Thanks


Solution

  • Android really isn't the right OS for a microcontroller. Above all else you want stability for that. Android won't give you that- any process can be killed at any time for resources by the OS. The other problem is as you see, garbage collection. This can occur at any time and block any or all threads. If you need to quickly respond to hardware events, you don't want to use a garbage collected language- you want something that you can assure will never be blocked. I'd rethink this approach.

    In a normal app, I'd say the GC events you see are ok as long as you aren't seeing perf issues. But for a microcontroller, you can definitely lose events due to it. You won't crash from those unless you run out of memory, but they can delay everything.