1

I don't have an error or crashes but i always see this message on my log on my android studio "The application may be doing too much work on its main thread.". Can someone enlighten me with log message. Maybe i am doing something wrong on my coding or should i just don't mind this?

MaroonDre
  • 43
  • 1
  • 7
  • 1
    Does this answer your question? [The application may be doing too much work on its main thread](https://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread) – YannickIngenierie Aug 06 '21 at 13:00

2 Answers2

2

In a way, this could be the answer to your question as well

First off, you need to be aware of 2 concepts:

Synchronous : will run in the same thread.

Asynchronous: will run in a different thread.

There's a few things possible here, one of them is that using the app in debug mode / running with debug build variant contains plenty of logs and slows down the app, which can cause this message - while actual users never see an issue with this since those logs are automatically removed(and you should clean up your personal logs before release as well).

Another one - usually when you're new to android development you write all the code on the main thread, which works fine up to some limit, but when you start implementing complex operations in the app(e.g. loading large images/files, downloading images/data synchronously - but mind you, this is usually asynchronous when you're using a popular library for networking/image loading normally, you can use them synchronously when needed though as well) this can become a problem. This usually happens as you build up on your app.

By default, everything you write in the code is being run on the UI thread(synchronously). This causes android to render UI updates and code operations simultaneously. The proper way of doing things in android is that you do all the complex operations asynchronously, and only when all those operations are completed, you update the UI on the main thread with completed data.

( Because the screen is refreshed every 16 ms (1 s/60 fps = 16 ms per frame), it is crucial to ensure that all of your rendering can occur in less than 16 ms.)

If the rendering takes longer than 16ms, that message pops up at times.

It's a bit hard to follow this rule at all times, but with some practice comes perfection, and when doing it like this, end-users will pretty much never experience stuff like ANR's or UI freezes. Of course, it's not all sunshine and rainbows, doing things asynchronously brings its own set of problems. If you're not familiar with these concepts, you should read up on synchronous/asynchronous, threading, and kotlin coroutines. Lifecycles(activity/fragment/in general) and Support different screen sizes could be helpful as well

CoffeeCode
  • 151
  • 2
  • 7
0

It's just a simple message indicating that your app code taking long to process and it's skipping frames due to that. It happens most of the time as there is always something in the normal code that's not utilized well for the RAM & CPU.

But it should not be a problem if the the number of frames skipped are fairly small (<100). But if the number of frames skipped and large and in the order of 300+ then there can be some serious trouble with your code.

So try utilizing tasks which are being done on the main thread. Perform data loading & other background tasks on the background task & UI related tasks on the Main thread.

Dev4Life
  • 2,328
  • 8
  • 22