1

I create a new thread in which I do a lot of work for about 15 seconds while at the same time, there is a rotating arrow in the center of the screen.

the problem is, the rotate animation is not smooth.

I run a performance test with traceview and get the following graph: performaceGraph

in the above graph, the first row is the main UI thread, and the second row is my own thread, from which you can see at the beginning 4 seconds, my own thread work load is very little and main thread can take almost all the CPU time to do animation rendering, so the animation is fine.

But in the following 1.5s, my thread work load is getting heavy and there are a few white blocks in the main thread which indicates the animation is paused for a little period of time, because CPU is taken away from main thread to my thread to do the heavy work. That's why the animation is not smooth.

I traced the part and found out that BouncyCastleProvider.init took most of the work, and the coresponding code is as follows:

SecretKey k = new SecretKeySpec(key.getBytes(), "AES");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, k);
finalStr = new String(Base64.encode(cipher.doFinal(originalStr.getBytes())));

But I can't find other way to do AES256Encrypt without using BouncyCastleProvider, and I don't know if there's other way to get a smooth animation, so I hope I can get some ideas from you to optimise the program, thanks!

Wood
  • 945
  • 1
  • 9
  • 18

2 Answers2

1

It's probably not so much the CPU your worker thread is taking, but I bet it's churning a lot of memory, and it's the Java garbage collection that's causing your animation thread to stutter. Dalvik can do concurrent GC, but there are brief periods where it pauses all threads, and some GCs are not concurrent. Have a look at your ADB logcat output and look for GC messages. I don't know the details of BouncyCastleProvider, so I don't know how dirty it is in terms of allocating objects, but there's probably not a lot you can do for it.

Something to be careful about is to try very hard to avoid ANY sort of Java allocations in your animation thread. That will reduce the number of dead objects that pile up for the GC to deal with, and will decrease the chances that some off-animation-thread GC will make your animation stutter. The memory tools in DDMS can help you look for objects being allocated; many of them are not obvious.

As a side note, you can look at setting thread priorities to give your worker thread a lower priority and make sure it doesn't starve the animation thread, but that won't help with GC pauses -- those stop the world. So it may not help your problem at all.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
1

Try putting the BC init stuff into a service and then use the manifest to put that service into a separate process.

Its a wild guess , but that separation into 2 processes which normally will never happen in an app, may resolve some resource contention ( cpu, memory ).

Community
  • 1
  • 1
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43