Android and Java: Reduce memory usage on a service loop

前端 未结 2 1529
猫巷女王i
猫巷女王i 2021-01-16 08:41

I have an Android Service which updates a notification every second using this Thread (comments are not really relevant):

thread = new Thread() {
    @Overri         


        
相关标签:
2条回答
  • 2021-01-16 08:46

    In finally block you can do the following thing

     finally {
    try {
        freeOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2021-01-16 08:46

    how can I lower memory usage in a more eficient way?

    The big thing would be to get rid of all of this process-forking nonsense and replace it with a simple call to getMemoryInfo(). Or, use JNI to call some C APIs to get your memory information. Forking a process every second is exceedingly wasteful on a mobile device. Besides, you have no way of knowing if free exists on every device, as on-device command-line binaries are not part of the Android SDK and are not guaranteed to be consistent.

    Secondarily, use StringBuilder or String.format() rather than string concatenation to build up your Notification text.

    0 讨论(0)
提交回复
热议问题