简体   繁体   中英

My Android app hangs even though there is no UI thread tasks, memory and processor usage are ok

I am developing an android app. Each activity binds to a service that is used to do all the actual work. Normally, everything works great - but not when an array with about 100 000 objects arrives from the server as an update.

After about 10 000 objects are inserted the DB thread and even the UI thread hangs. The processor load is never more than 60%, memory used is less than 200 MB.

I do all my DB work in a separate thread on the service; I make sure not to overload the Db queue, extracting objects from the JsonArray just enough so that there are always about 100 objects in DB thread queue.

When the big array with updates arrives from the server as a Json Array, I gradually disassemble it and work with each object separately.

Db queue uses Room to access the database.

`@Update(onConflict = OnConflictStrategy.FAIL)`
`public abstract void update(User localTransport);`

There are no transactions.

Ids of all DB objects are stored in a HashMap so I may choose to update or insert a row with no overhead.

Here is my update function:

private long doUpdateOrInsert(Sendable s) {
    if (Core.core.dbHelper.storage.hasItem(s)) {
        dbDao.updateSendable(s);
        return s.id;
    }
    else {
        long id = this.dbDao.insert(s);
        Core.core.dbHelper.storage.addItem(s,id);
        return id;
    }
}

Please, could you tell me how to solve this problem? Why does my UI thread hang? I don't get anything unusual in logs except for ANR cause UI is unresponsive for five seconds and my logs that my DB thread stops processing messages.

Edit:

Sorry for not stating clearly, all my work is done on a Service. It is separate from activities, launched with startService() , and is bound to the current activity. Db Thread is run by a separate thread in the Service.

Here are the screenshots of profiler after app randomly died and returned to the previous screen even though I din't touch the phone, without even writing anything to the app logs.

Threads

Memory

也许尝试使用rxJava进行数据库操作(对线程使用调度程序)并使用事件更新ui

Use an IntentService , so that the work is done on a background thread.

I tried to make a thread that will do the upload/download but once the Service is finished Android kill my Thread.

Which is why you should use an IntentService, as Android manages both the background thread and stopping the Service for you. You just send it work via

startService()

and do the work in onHandleIntent().

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM