The main thread task is too many to cause an exception to exit (the application may doing too much work in its main thread)

Source: Internet
Author: User

It took a day to solve the bug today.

The problems that occur during the run of the program are tricky and more difficult to solve if there are no more rules.

Fortunately this bug is by law, that is, after the execution of the program for about half an hour or so will cause the program to quit; then find the next reason on the Internet, nothing but to say a few points:

1, the business in the sub-thread to complete, and then through the handler to update the interface

2, through the Runonuithread method to achieve

One more thing to add: Optimize the code, do not need to repeat the execution of the code once is OK, especially the need to draw the UI code more can not be placed in the place of repeated execution

First, bug site restore

My program is like a Web image browser that automatically plays pictures

Through the timer to every 10s to perform a picture of the network request, the picture is set to Imagview will be displayed after the image, through these two steps to complete the basic requirements.

However, when the program was running for 30 minutes, that is, after playing more than 100 different photos, there appeared "skipped frames! The application may is doing too much work in its main thread "bug, accompanied by Gc_for_alloc freed 2755K, 18% free 13874k/16884 K, paused 3753ms, total 3756ms "Such a mistake

Second, the cause of the occurrence of the bug

From the reported exception itself, the meaning is that the main thread task is too heavy, memory consumption too much. But I'm using bitmaputils. This tool library, only the first time the network request, then will use the default cache data

So the problem certainly is not in the network request data this piece, that is in ImageView set picture resources here.

Look carefully, there should be no problem ah, just a few code:

1Linearparams =NewRelativelayout.layoutparams (2 Commonutils.getscreenwidth (Contextshowcall), Commonutils.getscreenheight (Contextshowcall)); 3 Imageview.setlayoutparams (Linearparams);
4String Imgurl = "http://" +qiniubucketname+ ". com1.z0.glb.clouddn.com/image-" +pic_casted_index+ ". jpg";5 6Relativelayoutshowcall.setbackgrounddrawable (Newbitmapdrawable (Lastbitmap));7Bitmaputils.display (ImageView, Imgurl, Bigpicdisplayconfig, callback);

From the code point of view, the first 3 lines of code, the ImageView in the other parts of the program without the premise of its modification, is not to be set repeatedly. Put the first three lines of code where the program was initialized, and then run the program, and find that the problem has not been discovered for 2 hours.

The problem is locked in the 3 lines of code: repeatedly applying variables from memory, repeating the width and height of the control, plus the need to redraw the picture, for the main thread is really a heavy task, then by adjusting the code does reduce the main thread of the task volume (just Redraw imageview picture).

Summarize:

1, for the program during the operation of the bug, to find the law, one way to find the law is to execute the same program in multiple devices simultaneously, so as to quickly locate the error occurrence of the point of time and error log

2, preferably in the program exception can be guaranteed 100% to send abnormal data to the developer mailbox or management tools, I am sorry, I have not found a can meet the conditions (foreign mint Splunk, domestic Friends of the League do not 100%)

3, Eclipse Logcat by default can only cache 5,000 data, this can be set, so as to facilitate viewing system log:

The main thread task is too many to cause an exception to exit (the application may doing too much work in its main thread)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.