0

I ran a Traceview on my Android app and the result was terrible: 2200msec. I've talked with a lot of people and have been told to go up or down the stack and find the offending code. The problem is, when I go all the way up, or all the way down the stack, there's no obvious indicator as to why (I know you're scoffing, because you're right, there is a reason, but please read-on I'm new).

If I look at Excl CPU time, BitmapFactory.nativeAssetDecode is taking a huge amount of time, well over 1400msec. Obviously this is a major part of the problem with my Activity, however, identifying where this is coming from has been a nightmare. None of my "direct" code is anywhere near (not a Child, nor Parent) this part of the stack, in fact, all my "direct" methods appear to be well-behaved, firing and finishing in mere 0-4 msec as would be expected.

One thing I've found is if I start my Traceview AFTER setContentView(), the Traceview log drops to just 90 msec. I'm honestly too new to understand this result, I know this is misleading because of course setContentView() takes time, but perhaps my layout is causing WAY too much time to be taken? Could my layout really be causing 2110 msec?

This is where I'm confused. My layout has zero overdraw and appears to be a well-formed and non-redundant XML file. My biggest layout has 41 view widgets in it, I swear I've seen many well-performing Activities with over a 100 view widgets in them. My view is designed of essentially 4 Layouts and 36 view widgets (TextViews, etc), each of these items has a Style assigned to them from the Style.xml. I hope I haven't taken things for advantage and created a monster view?

Perhaps if someone could expand on the theory to trace issues when they aren't caused by direct code you write, or the theory behind isolating the cause of the CPU time for "runaway" methods, I'd be able to better help myself (and God knows I've tried, this entire weekend in fact).

TL;DR If I start my Traceview AFTER onCreate's setContentView() it takes 2110 msec less time to load my activity than if I put it BEFORE setContentView(). My Activity's view isn't exactly complicated though, so I'm confused.

I appreciate it so much, Ryan

AutoM8R
  • 3,020
  • 3
  • 32
  • 52

2 Answers2

1

One of the things that can really hurt you is to have layouts whose measuring affect each other. I would be that you are using lots of "weights" in your layout, as these are a bit time intensive to calculate.

You can analyze your slow layout using hierarchyviewer. This is in the Android tools menu in IntelliJ or in Eclipse. It only runs on test devices, or with apps that have added ViewServer (see this for instructions)

One note-- green, yellow, and red are relative to your current view Hierarchy. Here is a note from the documentation:

These indicators can be red, yellow, or green and represent how each view renders in relation to other views in the tree. They are not a strict representation of a bad or good view, per se.

A red dot means that this view renders the slowest, compared to all views in your hierarchy.
A yellow dot means that this view renders in the bottom 50% of all views in your hierarchy.
A green dot means that this view renders in the top 50% of all views in your hierarchy.

It really is just an indication of what you should try to fix.

Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
  • Hi HalR, I'm just using 1 weight in my layout, when I run Hierarchy Viewer it's all green except for a couple yellows and reds. Would that be enough to cause a 1.5 - 2.0 second delay? Thank you! Also, Lint does not recommend any changes to my layout. It appears to be a nice flat layout too in Hierarchy Viewer. I have no idea what's going on, but I do note, all my activities seem to have a common pause of around 1 second, they share Style.xml only, could there be a problem with my Style? – AutoM8R Jun 11 '13 at 06:02
  • Style could be bad. What do you have going on in there? – HalR Jun 11 '13 at 06:08
  • that's the thing, I just use the base Holo Light theme, and then have about 30 custom Styles. I removed all styling from my XML layout (except for the base style which applies to everything of course) and it was still slow to load! I'm at a loss, Lint has no suggestions and everything looks good in all tools except Traceview showing me that big waste of CPU setContentView(). :( – AutoM8R Jun 11 '13 at 17:10
  • Did you see my edit to my answer. The green/red/yellow values are relative to your view. You should see if there is anything you can do to speed up your red items. – HalR Jun 11 '13 at 17:29
  • Thank you sir, I appreciate your follow-up. The reds are due to the fact they are the layouts holding all my views, so I guess red in this case ins't necessarily bad. The yellows are similar, only my main layouts have colors other than green, every other view widget is green/green/green. In all counts, it looks like my layout is above average for Hierarchy Viewer results. Something is going on in setContentView() though, this is very strange. The only common element to all of the delays in my Activities is the base Holo Light theme, I wonder... – AutoM8R Jun 11 '13 at 23:08
  • Well, I found the problem and it's bitter sweet. It's sweet because it wasn't my code nor layout causing the problem, it was the admob AdView using the loadAdOnCreate="true" to create ads. It's bitter, because I now may have to switch sources of revenue if I can't remove the loading delay created by the AdView! – AutoM8R Jun 12 '13 at 01:52
1

Well, I found the problem and it's bitter sweet. It's sweet because it wasn't my code nor layout causing the problem, it was the admob AdView using the loadAdOnCreate="true" to create ads. It's bitter, because I now may have to switch sources of revenue if I can't remove the loading delay created by the AdView!

AutoM8R
  • 3,020
  • 3
  • 32
  • 52
  • Good to hear you figured it out. I'm glad I looked into it, because I learned a bit more about layout slowness. – HalR Jun 12 '13 at 03:06
  • Me too, you know what really worked here, I should have isolated the variables. I should have realized that beyond Styles, Ads were shared across layouts. If I had deduced this more logically, I could have saved myself days of testing. Alas, I learned a lot myself, and I thank you for being so kind to reply, not many do! – AutoM8R Jun 12 '13 at 03:10