Stopped Service is Doing Constant Garbage Collection

后端 未结 3 2021
野性不改
野性不改 2020-12-15 17:32

I am developing an application which has many services. When I stop the intent service, all threads and service should be stopped but the UI is hung and the following errors

相关标签:
3条回答
  • 2020-12-15 18:06

    I Don't see a reason for using Intent service when there's need for the service to be stopped. But if you still think it has to be done then either make call to stopself() from onhandleIntent itself (which will again break the protocol), or you can call it from your activity(which is prone to stop queue processing which is pending).

    i Would rather go with bounded services if i have to manage their whole lifecycle. Here's the reference::

    http://developer.android.com/guide/topics/fundamentals/services.html

    0 讨论(0)
  • 2020-12-15 18:09

    Profile your app - I found the cause of my similar problem that way.

    Android Studio: Tools -> Android -> Android Device Monitor

    Select your app and it the top (left) select:

    Start method profiling

    and after some time stop it the see the result.

    0 讨论(0)
  • 2020-12-15 18:24

    Same problem also occured for me. But I am using SqLite. When I want to select List of my entity I forgot to add some important methods. When problem occured my code is following

     public List<Blocked> getBlockeds() {
        List<Blocked> blockeds = new ArrayList<>();
        String[] allColumns = {"id", "phoneNumber"};
        Cursor cursor = database.query("Blockeds", allColumns, null, null, null, null, "id desc");
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Blocked blocked = new CursorToPojo().cursorToBlocked(cursor);
            blockeds.add(blocked);
        }
        return blockeds;
    }
    

    When change code to following the problem solved.

    public List<Blocked> getBlockeds() {
        List<Blocked> blockeds = new ArrayList<>();
        String[] allColumns = {"id", "phoneNumber"};
        Cursor cursor = database.query("Blockeds", allColumns, null, null, null, null, "id desc");
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Blocked blocked = new CursorToPojo().cursorToBlocked(cursor);
            blockeds.add(blocked);
            cursor.moveToNext();
        }
        cursor.close();
        return blockeds;
    }
    

    I realised that when I did not use cursor.moveToNext cursor never end and the preceding problem start.

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