Android development issues - frequent calls to GC when handling strings

Occurrence scenarios

When TCP is used to communicate with the server, picture transmission is needed. Byte stream is used and bytes need to be spliced. The key is to encrypt the message after splicing. The parameters of the encryption method are strings. And I also used the "+" form of string for splicing, encryption and subcontracting.

describe

When debugging the software, the terminal calls GC frequently, and the processing speed is abnormally slow. The following are the contents of the log:

04-23 11:22:00.370 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 633(23KB) AllocSpace objects, 277(10MB) LOS objects, 39% free, 7MB/12MB, paused 1.351ms total 121.955ms
04-23 11:22:00.656 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 389(13KB) AllocSpace objects, 225(9MB) LOS objects, 39% free, 8MB/13MB, paused 1.188ms total 125.609ms
04-23 11:22:00.994 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 426(15KB) AllocSpace objects, 274(11MB) LOS objects, 40% free, 8MB/13MB, paused 5.150ms total 144.439ms
04-23 11:22:01.718 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 505(17KB) AllocSpace objects, 328(13MB) LOS objects, 40% free, 8MB/13MB, paused 1.200ms total 148.203ms
04-23 11:22:02.044 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 384(13KB) AllocSpace objects, 240(9MB) LOS objects, 40% free, 9MB/15MB, paused 5.214ms total 131.344ms
04-23 11:22:02.410 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 461(16KB) AllocSpace objects, 300(12MB) LOS objects, 39% free, 8MB/14MB, paused 1.005ms total 150.486ms
04-23 11:22:02.746 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 405(14KB) AllocSpace objects, 255(10MB) LOS objects, 40% free, 9MB/15MB, paused 5.015ms total 129.852ms
04-23 11:22:03.133 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 441(15KB) AllocSpace objects, 283(12MB) LOS objects, 40% free, 8MB/13MB, paused 5.585ms total 156.783ms
04-23 11:22:03.457 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 383(13KB) AllocSpace objects, 237(10MB) LOS objects, 39% free, 8MB/14MB, paused 2.303ms total 138.655ms
04-23 11:22:03.785 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 402(14KB) AllocSpace objects, 244(11MB) LOS objects, 40% free, 8MB/13MB, paused 1.129ms total 124.835ms
04-23 11:22:04.120 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 381(13KB) AllocSpace objects, 237(10MB) LOS objects, 39% free, 8MB/14MB, paused 5.454ms total 124.930ms
04-23 11:22:04.486 2448-2463/com.beviswang.dttimer I/art: Background partial concurrent mark sweep GC freed 427(15KB) AllocSpace objects, 263(11MB) LOS objects, 39% free, 8MB/13MB, paused 1.007ms total 145.510ms

Reason

Splicing long strings with "+" can seriously affect performance. It is found that when "+" is used to splice a certain amount of character strings on high-performance devices, GC will not be called frequently. However, using this method to splice on low-performance devices will lead to a very time-consuming splicing process. After all, GC recycling is called frequently.

Solution

String builder is used to splice long strings, append is used to splice strings at the end, and insert is used to splice strings at any position.

Posted by Mirrorball on Mon, 30 Mar 2020 22:53:49 -0700