I am developing an app which needs to perform some CPU intensive calculations in background threads. On Android versions 12 and higher (tested in the emulator and on physical devices), I noticed that the app performance is very inconsistent – to the point where the exact same calculation may sometimes take 10 times as long. Profiling the app reveals that in these instances the CPU usage is also lower than expected, as if the system allocates less resources to the app.
For developing purposes, I have the app installed twice with different package names. Even with identical code bases, both packages can exhibit different behavior performance wise. Restarting the app or the system may change the behavior, but this is not guaranteed.
Changing the battery optimization setting does not yield any effect.
Any suggestions on what the issue may be or how to investigate further?