Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of high and low memory of Android program

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to solve the ups and downs of Android program memory". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One: background

1. Tell a story

Yesterday to continue to pay the technical debt, optimize a round of the program to pull online after the memory continues to rise and fall, low when 20G, high when 30G, after a while and then dropped a few G, there is no doubt that there is any collection or what operation in the program takes up a lot of memory, so prepare to catch dump analysis when 28,29G.

Second, the solution.

Finding a problem from a snapshot is like seeing a doctor. According to the guess of the disease, there is a set of experience to follow.

1. Find out the objects > 10m in the managed heap

It is usually the easiest to deal with large collections from the managed heap. To see which type takes up more space, it basically has a problem. In order to avoid typing out all types, filter is set here to kick out all less than 10m. You can use dumpheap-stat-min 10240 to desensitize sensitive objects.

0VR 000 >! dumpheap-stat-min 10240

Statistics:

MT Count TotalSize Class Name

00007ffe094e6fc0 4 523776 System.Object []

00007ffe094e6948 6 7179822 System.String

00007ffe0780da08 33 46514160 System.Collections.Generic.Dictionary`2 + Entry [[System.Int32, mscorlib], [System.Collections.Generic.HashSet`1 [[System.Int32, mscorlib]], System.Core] []

00007ffe09f95f40 188739344 system. Collections.Generic.Dictionary`2 + Entry [[System.Int32, mscorlib], [System.Int32, mscorlib]] []

00007ffe094ec988 18 540828823 System.Byte []

00007ffe07802da8 1620 622578672 system. Linq.Set`1 + Slot [[System.Int32, mscorlib]] []

000001bc0452e600 1389 1038494910 Free

00007ffe094baf50 68 1128274800 system. Collections.Generic.Dictionary`2 + Entry [[System.Int32, mscorlib], [System.DateTime, mscorlib]] []

00007ffe094e9220 2224 1513951832 System.Int32 []

00007ffe07819df8 2232 1668042480 system. Collections.Generic.HashSet`1 + Slot [[System.Int32, mscorlib]] []

00007ffe094c8510 226 1672164568 System.Int64 []

00007ffdab8676e8 1137 1901228880 system. Collections.Generic.HashSet`1 + Slot [[System.Int64, mscorlib]] []

00007ffdab89b3b0 136 1986723840 system. Linq.Set`1 + Slot [[System.Int64, mscorlib]] []

Total 13321 objects

two。 Find suspicious objects in the heap

Because the program starts as an in-memory database, it is normal to have a large collection object containing the specified class. The penultimate line 7 has a Dictionary that takes up a lot of space. 1128274800 pound 1024 pound 1G, this does not seem to be the basic data, it should be an intermediate variable, the method table address is 00007ffe094baf50, through it you can find the memory addresses of these 68 collections.

0VO28 >! dumpheap-mt 00007ffe094baf50

Address MT Size

000001c2f262a708 00007ffe094baf50 69438000

000001c1bb8e1020 00007ffe094baf50 16147872

000001c1bce04760 00007ffe094baf50 33486336

000001c37e8f1020 00007ffe094baf50 143987328

000001c44e8f1020 00007ffe094baf50 287974800

000001c3c419b268 00007ffe094baf50 16147872

000001c3f6b9ac28 00007ffe094baf50 16147872

000001c467336fa0 00007ffe094baf50 33486336

000001c46f3fa760 00007ffe094baf50 69438000

000001c489df3668 00007ffe094baf50 16147872

000001c494166828 00007ffe094baf50 33486336

000001c4a68f1020 00007ffe094baf50 69438000

000001c4d4c5c290 00007ffe094baf50 16147872

000001c4da8f1058 00007ffe094baf50 33486336

000001c4de8f1020 00007ffe094baf50 69438000

000001c5028f1058 00007ffe094baf50 33486336

000001c5068f1020 00007ffe094baf50 33486336

...

The next step is to pick a few big Dictionary, such as this line: 000001c44e8f1020 00007ffe094baf50 287974800, and calculate the size:279M.

3. Find the code block where the collection is located

I know that the dictionary occupies 279m, but how do I know which code block this dictionary is in? To find the answer is also easy, through! gcroot to find its reference root, through the reference chain can find its code block, simply not too practical

0VOU 000 >! gcroot 000001c4de8f1020

Thread 2da8:

00000017f4c7e5d0 00007ffdab758ca1 xxxx.xxxx.xxxx.GetFlowAwayCustomer (Int32, System.String, system. Collections.Generic.Dictionary`2)

Rbp-238: 00000017f4c7e628

-> 000001c3d5c1bdf0 system .Collections.Generic.Dictionary`2 [[System.String, mscorlib], [System.Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]], mscorlib]]

-> 000001c3d8de7d10 System.Collections.Generic.Dictionary`2 + Entry [[System.String, mscorlib], [System.Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib], mscorlib]] []

-> 000001c3d8d58630 system. Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]]

-> 000001c4de8f1020 system. Collections.Generic.Dictionary`2 + Entry [[System.Int32, mscorlib], [System.DateTime, mscorlib]] []

From the above reference chain, you can see three pieces of information:

The current dictionary is on the 2da8 thread

Dictionary in the GetFlowAwayCustomer method, you can probably see that it is calculated to lose customers.

At the top of the call chain is the largest collection Dictionary, address:000001c3d5c1bdf0

4. Looking for more information

Dig up the contents of the dictionary

With the largest dictionary, let's take a look at the amount of memory consumed by the largest dictionary, Dictionary.

0VOU 000 >! objsize 000001c3d5c1bdf0

Sizeof (000001c3d5c1bdf0) = 340008256 (0x14441d40) bytes (system. Collections.Generic.Dictionary`2 [[System.String, mscorlib], [System.Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]], mscorlib]])

According to sizeof (000001c3d5c1bdf0) = 340008256 (0x14441d40) bytes calculation: 324m, Nima, this is one of the dictionaries, no wonder memory ups and downs, now we must particularly want to know what is inside, you can use da->! do to have a look inside the collection.

0start 000 >! da-length 1-start 1-details 000001c3d8de7d10

Name: System.Collections.Generic.Dictionary`2 + Entry [[System.String, mscorlib], [System.Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]], mscorlib] []

MethodTable: 00007ffdab940650

EEClass: 00007ffdab9405b8

Size: 192 (0xc0) bytes

Array: Rank 1, Number of elements 7, Type VALUETYPE

Element Methodtable: 00007ffdab940520

[1] 000001c3d8de7d38

Name: system. Collections.Generic.Dictionary`2 + Entry [[System.String, mscorlib], [System.Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]], mscorlib]]

MethodTable: 00007ffdab940520

EEClass: 00007ffe08e92920

Size: 40 (0x28) bytes

File: C:\ Windows\ Microsoft.Net\ assembly\ GAC_64\ mscorlib\ v4.0_4.0.0.0__b77a5c561934e089\ mscorlib.dll

Fields:

MT Field Offset Type VT Attr Value Name

00007ffe094e9288 4003474 10 System.Int32 1 instance 58671583 hashCode

00007ffe094e9288 4003475 14 System.Int32 1 instance-1 next

00007ffe094ebf10 4003476 0 System.__Canon 0 instance 000001c2cec43610 key

00007ffe094ebf10 4003477 8 System.__Canon 0 instance 000001c3d7b45370 value

0VOU 000 >! do 000001c3d7b45370

Name: system. Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]]

MethodTable: 00007ffe094b9ec8

EEClass: 00007ffe08e9d528

Size: 80 (0x50) bytes

File: C:\ Windows\ Microsoft.Net\ assembly\ GAC_64\ mscorlib\ v4.0_4.0.0.0__b77a5c561934e089\ mscorlib.dll

Fields:

MT Field Offset Type VT Attr Value Name

00007ffe094e9220 4001858 8 System.Int32 [] 0 instance 000001c46e8f1020 buckets

00007ffe094baf50 4001859 10... ime, mscorlib]] [] 0 instance 000001c46f3fa760 entries

00007ffe094e9288 400185a 38 System.Int32 1 instance 2512598 count

00007ffe094e9288 400185b 3c System.Int32 1 instance 3194430 version

00007ffe094e9288 400185c 40 System.Int32 1 instance-1 freeList

00007ffe094e9288 400185d 44 System.Int32 1 instance 0 freeCount

00007ffe094dabb8 400185e 18... Int32, mscorlib]] 0 instance 000001bc06272ab8 comparer

00007ffe0a0463e0 400185f 20... eTime, mscorlib]] 0 instance 0000000000000000 keys

00007ffe0a046258 4001860 28... eTime, mscorlib]] 0 instance 0000000000000000 values

00007ffe094e6f28 4001861 30 System.Object 0 instance 0000000000000000 _ syncRoot

You can see the seven elements in the big dictionary, and then I chose an embedded Dictionary. I can see the count=251w of the embedded dictionary, and I won't output the details in it.

Thread digging stack

With the contents of the dictionary, let's continue to look at what the thread [2da8] is doing at this time.

0RV 028 > ~ [2da8] s

Ntdllqualified NtWaitForSingleObject0x14:

00007ffe`28646124 c3 ret

0VO28 >! clrstack

OS Thread Id: 0x2da8 (28)

Child SP IP Call Site

00000017f4c7e388 00007ffe28646124 [HelperMethodFrame: 00000017f4c7e388]

00000017f4c7e4f0 00007ffe09e48e52 system. Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]] .Resize (Int32, Boolean)

00000017f4c7e560 00007ffe09316c65 system. Collections.Generic.Dictionary`2 [[System.Int32, mscorlib], [System.DateTime, mscorlib]] .insert (Int32, System.DateTime, Boolean)

We should know that if there is expansion, there will be wasted memory.

This is the end of the content of "how to solve the problem of high and low memory of Android program". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report