该应用程序可能在其主线程上做过多的工作

本文探讨了Android应用中主线程工作过多导致的问题,如日志cat重复显示警告。文章提供了多线程编程、提高性能的策略、解决主线程延迟的方法,包括使用AsyncTask和IntentService。此外,还提到了UI动画、数据库访问和资源大小对应用性能的影响,建议对资源文件大小和网络任务执行进行优化。
摘要由CSDN通过智能技术生成

本文翻译自:The application may be doing too much work on its main thread

I am new to Android SDK/API environment. 我是Android SDK / API环境的新手。 It's the first I am trying to draw a plot/chart. 这是我第一次尝试绘制图表。 I tried running different kinds of sample codes the emulator using 3 different free libraries, nothing is showing in the layout screen. 我尝试使用3个不同的免费库运行不同类型的示例代码模拟器,但布局屏幕中没有任何显示。 The logcat is repeating the following message: logcat重复以下消息:

W/Trace(1378): Unexpected value from nativeGetEnabledTags: 0
 I/Choreographer(1378): Skipped 55 frames!  The application may be doing too much work on its main thread.

The problem didn't persist and the chart worked when I ran a sample code pertaining to an evaluation copy of a licensed library. 当我运行与许可库的评估副本有关的示例代码时,问题仍然存在,并且图表起作用了。


#1楼

参考:https://stackoom.com/question/zaZV/该应用程序可能在其主线程上做过多的工作


#2楼

Try to use the following strategies in order to improve your app performance: 尝试使用以下策略来提高您的应用程序性能:

  • Use multi-threading programming if possible. 如果可能,请使用多线程编程。 The performance benefits are huge, even if your smart phone has one core (threads can run in different cores, if the processor has two or more). 即使您的智能手机具有一个内核(如果处理器具有两个或更多,线程也可以在不同的内核中运行),性能收益将是巨大的。 It's useful to make your app logic separated from the UI. 使您的应用程序逻辑与UI分离非常有用。 Use Java threads, AsyncTask or IntentService. 使用Java线程,AsyncTask或IntentService。 Check this . 检查一下
  • Read and follow the misc performance tips of Android development website. 阅读并遵循Android开发网站的其他性能提示。 Check here . 在这里检查

#3楼

taken from : Android UI : Fixing skipped frames 摘自: Android UI:修复跳过的帧

Anyone who begins developing android application sees this message on logcat “Choreographer(abc): Skipped xx frames! 任何开始开发android应用程序的人都会在logcat “ Choreographer(abc):xx跳帧! The application may be doing too much work on its main thread.” So what does it actually means, why should you be concerned and how to solve it. 因此,该应用程序可能在其主线程上做过多的工作。”因此,这实际上意味着什么,您为什么要担心以及如何解决它。

What this means is that your code is taking long to process and frames are being skipped because of it, It maybe because of some heavy processing that you are doing at the heart of your application or DB access or any other thing which causes the thread to stop for a while. 这意味着您的代码需要花费很长时间来处理,因此帧被跳过,这可能是因为您在应用程序或数据库访问的核心处进行的繁重处理或任何其他导致线程执行错误的操作停一会儿。

Here is a more detailed explanation: 这是更详细的说明:

Choreographer lets apps to connect themselves to the vsync, and properly time things to improve performance. Choreographer允许应用程序将自身连接到vsync,并适当安排时间以提高性能。

Android view animations internally uses Choreographer for the same purpose: to properly time the animations and possibly improve performance. Android视图动画在内部使用Choreographer来达到相同的目的:正确地设置动画时间并可能改善性能。

Since Choreographer is told about every vsync events, I can tell if one of the Runnables passed along by the Choreographer.post* apis doesnt finish in one frame's time, causing frames to be skipped. 由于Choreographer被告知每个vsync事件,因此我可以判断Choreographer.post * api传递的Runnable之一是否在一帧时间内未完成,从而导致跳过帧。

In my understanding Choreographer can only detect the frame skipping. 以我的理解,编舞只能检测到跳帧。 It has no way of telling why this happens. 它无法说明为什么会这样。

The message “The application may be doing too much work on its main thread.” could be misleading. 消息“应用程序可能在其主线程上做太多工作。”可能会误导您。

source : Meaning of Choreographer messages in Logcat 来源: Logcat中编舞者消息的含义

Why you should be concerned 为什么要担心

When this message pops up on android emulator and the number of frames skipped are fairly small (<100) then you can take a safe bet of the emulator being slow – which happens almost all the times. 当此消息在android模拟器上弹出并且跳过的帧数很小(<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. 但是,如果跳过的帧数很大且在300+左右的数量级,那么您的代码可能会遇到一些严重的麻烦。 Android devices come in a vast array of hardware unlike ios and windows devices. 与ios和Windows设备不同,Android设备具有多种硬件。 The RAM and CPU varies and if you want a reasonable performance and user experience on all the devices then you need to fix this thing. RAM和CPU有所不同,如果要在所有设备上获得合理的性能和用户体验,则需要修复此问题。 When frames are skipped the UI is slow and laggy, which is not a desirable user experience. 跳过帧时,UI缓慢且缓慢,这不是理想的用户体验。

How to fix it 如何修复

Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. 要解决此问题,需要确定存在或可能发生长时间处理的节点。 The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. 最好的方法是执行与主UI线程分开的线程中的大小无关的所有处理。 So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread 因此,无论是从SQLite数据库访问数据,还是做一些硬核数学,或者只是对数组进行排序–都可以在另一个线程中进行

Now there is a catch here, You will create a new Thread for doing these operations and when you run your application, it will crash saying “Only the original thread that created a view hierarchy can touch its views“. 现在这里有一个问题,您将创建一个用于执行这些操作的新线程,并且在您运行应用程序时,它会崩溃,提示“只有创建视图层次结构的原始线程才能触摸其视图”。 You need to know this fact that UI in android can be changed by the main thread or the UI thread only. 您需要知道以下事实:Android中的UI只能由主线程或UI线程更改。 Any other thread which attempts to do so, fails and crashes with this error. 尝试这样做的任何其他线程都会失败,并因该错误而崩溃。 What you need to do is create a new Runnable inside runOnUiThread and inside this runnable you should do all the operations involving the UI. 您需要做的是在runOnUiThread内创建一个新的Runnable,并在此Runnable中执行涉及UI的所有操作。 Find an example here . 在此处查找示例。

So we have Thread and Runnable for processing data out of main Thread, what else? 所以我们有Thread和Runnable来处理主线程中的数据,还有什么呢? There is AsyncTask in android which enables doing long time processes on the UI thread. android中有AsyncTask,它可以在UI线程上执行长时间的处理。 This is the most useful when you applications are data driven or web api driven or use complex UI's like those build using Canvas. 当您的应用程序是数据驱动的或Web api驱动的,或者使用复杂的UI(例如使用Canvas构建的UI)时,这是最有用的。 The power of AsyncTask is that is allows doing things in background and once you are done doing the processing, you can simply do the required actions on UI without causing any lagging effect. AsyncTask的功能是允许在后台执行操作,完成处理后,您可以简单地在UI上执行所需的操作,而不会造成任何滞后效果。 This is possible because the AsyncTask derives itself from Activity's UI thread – all the operations you do on UI via AsyncTask are done is a different thread from the main UI thread, No hindrance to user interaction. 这是可能的,因为AsyncTask本身是从Activity的UI线程派生的-您通过AsyncTask在UI上执行的所有操作都是与主UI线程不同的线程,不妨碍用户交互。

So this is what you need to know for making smooth android applications and as far I know every beginner gets this message on his console. 因此,这是制作流畅的android应用程序所需的知识,据我所知,每个初学者都会在他的控制台上收到此消息。


#4楼

As others answered above, "Skipped 55 frames!" 正如其他人在上面回答的那样,“跳过了55帧!” means some heavy processing is in your application. 意味着您的应用程序正在处理一些繁重的工作。

For my case, there is no heavy process in my application. 就我而言,我的申请没有繁琐的过程。 I double and triple checked everything and removed those process I think was a bit heavy. 我对所有内容进行了两次和三次检查,并删除了我认为有些繁重的那些过程。

I removed Fragments, Activities, Libraries until only the skeleton was left. 我删除了“片段”,“活动”和“库”,直到只剩下骨骼。 But still the problem did not go away. 但是问题仍然没有解决。 I decided to check the resources and found some icons and background I use are pretty big as I forgot to check the size of those resources. 我决定检查资源,发现我使用的一些图标和背景很大,因为我忘记了检查这些资源的大小。

So, my suggestion is if none of the above answers help, you may also check your resource files size. 因此,我的建议是,如果以上答案均无济于事,您还可以检查资源文件的大小。


#5楼

I am not an expert, but I got this debug message when I wanted to send data from my android application to a web server. 我不是专家,但是当我想将数据从我的Android应用程序发送到Web服务器时,却收到了此调试消息。 Though I used AsyncTask class and did the data transfer in background, for getting the result data back from server I used get() method of the AsyncTask class which makes the UI synchronous which means that your UI will be waiting for too long. 尽管我使用了AsyncTask类并在后台进行了数据传输,但是为了从服务器获取结果数据,我使用了AsyncTask类的get()方法,该方法使UI同步,这意味着您的UI将等待太长时间。 So my advice is to make your app do every network oriented tasks on a separate thread. 因此,我的建议是使您的应用程序在单独的线程上执行所有面向网络的任务。


#6楼

My app had same problem. 我的应用有同样的问题。 But it was not doing other than displaying list of cards and text on it. 但是除了显示卡片列表和文本之外,它没有做其他事情。 Nothing running in background. 什么都没有在后台运行。 But then after some investigation found that the image set for card background was causing this, even though it was small(350kb). 但是随后经过一些调查发现,尽管背景很小(350kb),但为卡片背景设置的图像却是造成这种情况的原因。 Then I converted the image to 9patch images using http://romannurik.github.io/AndroidAssetStudio/index.html . 然后,我使用http://romannurik.github.io/AndroidAssetStudio/index.html将图像转换为9patch图像。
This worked for me. 这对我有用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值