B
B
Borsh-na-stole2015-08-28 16:47:39
Android
Borsh-na-stole, 2015-08-28 16:47:39

Performance degradation in Android app due to GC?

In a typical piece of code, I have a performance drop of 5 seconds.

for (int i = 0; i < list.size(); i++){        \\ size ~ 30k
        String str = list.get(i);
        float strWidth = paint.measureText (str);
}

Tested in gyenni motion emulator. In a real device, processing takes as much as 50 seconds. But if I do this:
for (int i = 0; i < list.size(); i++){        \\ size ~ 30k
        String str = list.get(i);
         
        str = "a very very very long string";
        float strWidth = paint.measureText (str);
}

That, there are no problems. How can this be explained? Help me please.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Bolshakov, 2015-08-29
@Borsh-na-stole

If accuracy is not important, then you can do this:

float symWidth = paint.measureText("X");
for (int i = 0; i < list.size(); i++){        \\ size ~ 30k
        String str = list.get(i);
        float strWidth = symWidth * str.length;
}

Accuracy is far from ideal if the font is not monospaced, but fast.

G
GavriKos, 2015-08-28
@GavriKos

The problem is most likely all the same in get and the loop. And the second example does not slow down because get does not happen - optimization. The cycle itself may not even occur.

B
Borsh-na-stole, 2015-08-28
@Borsh-na-stole

I'll comment out the line with measureText in 1 example and everything is ok, it works fast.
Rendered Strings str; and int size = list.size() per loop - didn't help. I parallelized the code using AsyncTask, it did not help (each thread processed a separate part of the list). I also tried an array for the place of the list, and the file stream.readLine () does not help.
Is the measureText function slow?

I
IdeSade, 2015-08-28
@IdeSade

measureText is very slow!
I have no idea why to consider the size for all rows at once ...
Maybe an error in the logic?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question