用long类型让我出了次生产事故,写代码还是要小心点
昨天發(fā)現(xiàn)線上試跑期的一個(gè)程序掛了,平時(shí)都跑的好好的,查了下日志是因?yàn)樽蛱爝\(yùn)營(yíng)跑了一家美妝top級(jí)淘品牌店,會(huì)員量近千萬,一下子就把128G的內(nèi)存給爆了,當(dāng)時(shí)并行跑了二個(gè)任務(wù),沒轍先速寫一段代碼限流,后面再做進(jìn)一步優(yōu)化。
一:背景
1. 背景介紹
因?yàn)槭亲约簩懙拇a,所以我知道問題出現(xiàn)在哪里,如果大家看過我之前寫的文章應(yīng)該知道我用全內(nèi)存跑了很多模型對(duì)用戶打標(biāo)簽,一個(gè)模型就是一組定向的篩選條件,而為了加速處理,我會(huì)原子化篩選條件,然后一邊查詢一邊緩存原子化條件獲取的人數(shù),后面的模型如果命中了前面模型的原子化條件,那么可以直接從緩存中讀取它的人數(shù)即可,這也是動(dòng)態(tài)規(guī)劃的思想~ ,如果不明白我來畫張圖。
從上面圖可以看到,在計(jì)算模型2的時(shí)候,條件1的人數(shù)可以直接從模型1下的條件1處獲取,模型三下的2,5的人數(shù)也可以直接從模型1和2處獲取,這樣就大大加速的處理速度。
2. 找原因
剛才提到了緩存人數(shù),我也不知道為什么用了這么一個(gè)類型,如下代碼:
/// <summary>/// 緩存原子人群/// key: 原子化條件/// value: 人數(shù)集合/// </summary>public ConcurrentDictionary<string, List<long>> CachedCrowds { get; set; } = new ConcurrentDictionary<string, List<long>>();我說的是里面的List\,我居然用了long類型存儲(chǔ)customerID,可能是看了這個(gè)項(xiàng)目先祖原先定義的long才跟風(fēng)成long,????????????,誰家店有數(shù)不盡的客戶,國(guó)家才14億人呢,而一個(gè)long占用8個(gè)字節(jié),明顯是一種浪費(fèi)。
二:解決方案
1. 將long轉(zhuǎn)成int
人都是懶的,能少改點(diǎn)代碼就少改點(diǎn),省的背鍋,好事不出門,壞事傳千里,所以這里用int表示就足夠了,應(yīng)該能省一半的空間對(duì)不對(duì),接下來為了演示,在List\?和 List\?中分別灌入 500w 客戶ID,代碼如下:
public static void Main(string[] args){var rand = new Random();List<int> intCustomerIDList = Enumerable.Range(1, 5000000).OrderBy(m => rand.Next(0, 100000)).Take(5000000).ToList();List<long> longCustomerIDList = Enumerable.Range(1, 5000000).OrderBy(m => rand.Next(0, 100000)).Take(5000000).Select(m => (long)m).ToList();Console.WriteLine("處理完畢...");Console.Read();}接下來用windbg看一下他們?cè)诙阎懈髡级嗌賰?nèi)存。
~0s -> !clrstack -l -> !dumpobj 從主線程找到List\和List\?的局部變量,然后查看size。
0:000> ~0s ntdll!ZwReadFile+0x14: 00007ff8`fea4aa64 c3 ret 0:000> !clrstack -l OS Thread Id: 0x5b70 (0)Child SP IP Call Site 00000015c37feed0 00007ff889e60b9c ConsoleApp2.Program.Main(System.String[]) [C:\dream\Csharp\ConsoleApp1\ConsoleApp2\Program.cs @ 35]LOCALS:0x00000015c37fef90 = 0x0000014ad7c12d880x00000015c37fef88 = 0x0000014ad7c130600x00000015c37fef80 = 0x0000014ad7c3343800000015c37ff1a8 00007ff8e9396c93 [GCFrame: 00000015c37ff1a8] 0:000> !do 0x0000014ad7c13060 Name: System.Collections.Generic.List`1[[System.Int32, mscorlib]] MethodTable: 00007ff8e7aaa068 EEClass: 00007ff8e7c0b008 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 00007ff8e7a98538 400189e 8 System.Int32[] 0 instance 0000014af02d1020 _items 00007ff8e7a985a0 400189f 18 System.Int32 1 instance 5000000 _size 00007ff8e7a985a0 40018a0 1c System.Int32 1 instance 5000000 _version 00007ff8e7a95dd8 40018a1 10 System.Object 0 instance 0000000000000000 _syncRoot 00007ff8e7a98538 40018a2 0 System.Int32[] 0 shared static _emptyArray>> Domain:Value dynamic statics NYI 0000014ad61166c0:NotInit << 0:000> !do 0000014af02d1020 Name: System.Int32[] MethodTable: 00007ff8e7a98538 EEClass: 00007ff8e7c05918 Size: 33554456(0x2000018) bytes Array: Rank 1, Number of elements 8388608, Type Int32 (Print Array) Fields: None0:000> !do 0x0000014ad7c33438 Name: System.Collections.Generic.List`1[[System.Int64, mscorlib]] MethodTable: 00007ff8e7aad2a0 EEClass: 00007ff8e7c0bd70 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 00007ff8e7aa6c08 400189e 8 System.Int64[] 0 instance 0000014a80001020 _items 00007ff8e7a985a0 400189f 18 System.Int32 1 instance 5000000 _size 00007ff8e7a985a0 40018a0 1c System.Int32 1 instance 5000000 _version 00007ff8e7a95dd8 40018a1 10 System.Object 0 instance 0000000000000000 _syncRoot 00007ff8e7aa6c08 40018a2 0 System.Int64[] 0 shared static _emptyArray>> Domain:Value dynamic statics NYI 0000014ad61166c0:NotInit << 0:000> !do 0000014a80001020 Name: System.Int64[] MethodTable: 00007ff8e7aa6c08 EEClass: 00007ff8e7c09e50 Size: 67108888(0x4000018) bytes Array: Rank 1, Number of elements 8388608, Type Int64 (Print Array) Fields: None仔細(xì)看上圖,在主線程的堆棧中找到了三個(gè)變量,后兩個(gè)變量就是我們的List\?和 List\,分別是
Size: 33554456(0x2000018) bytes?=>?33554456/1024/1024 = 32M
Size:67108888(0x4000018) bytes?=>?67108888/1024/1024 = 64M
以后可以跟別人吹牛了,我知道500w個(gè)int占用是32M內(nèi)存,雖然內(nèi)存空間優(yōu)化了一半,但沒有本質(zhì)性的優(yōu)化,還得繼續(xù)往上挖,否則同時(shí)跑4個(gè)任務(wù)又要把內(nèi)存給爆掉了。。。
2. 使用bitarray
我們?cè)趯W(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)的時(shí)候,相信很多人都學(xué)習(xí)過bitmap,剛好原子化的篩選條件獲取的人數(shù)眾多,使用bitmap剛好滿足我的業(yè)務(wù)需求,如果不知道bitmap我簡(jiǎn)單解釋一下。
<1 style="box-sizing: border-box;"> 原理解釋
我們都知道一個(gè)int是4個(gè)字節(jié)。也就是4byte,也就是32bit,畫成圖就是32個(gè)格子,如下所示:
默認(rèn)情況下32個(gè)格子表示一個(gè)int是不是有點(diǎn)浪費(fèi),其實(shí)32個(gè)格子可以放置32個(gè)數(shù)字(1-32)。比如1放在第一個(gè)格子里,3放在第三個(gè)格子里。。。32放在第32個(gè)格子里,那么兩個(gè)int就可以存放1-64個(gè)數(shù)字,也就是說理想情況下可以優(yōu)化空間32倍,思維一定要反轉(zhuǎn)一下,把數(shù)字作為數(shù)組的下標(biāo),因?yàn)槭莃it,所以0,1兩種狀態(tài)剛好可以表示當(dāng)前格子是否已經(jīng)被設(shè)置了,1表示已設(shè)置,0表示未設(shè)置,好好品味一下,如果還是不明白,可以參考我八年前的文章:
經(jīng)典算法題每日演練——第十一題 Bitmap算法
在C#中已經(jīng)幫我們?cè)O(shè)置好了一個(gè)BitArray類,結(jié)合我剛才講得,大家好好品味一下bitarray如何向各自格子中設(shè)置值的,底層還是用m_array承載,它其實(shí)是一個(gè)int[]。
public void Set(int index, bool value){if (value){m_array[index / 32] |= 1 << index % 32;}else{m_array[index / 32] &= ~(1 << index % 32);}_version++; }public bool Get(int index){return (m_array[index / 32] & (1 << index % 32)) != 0; }<2 style="box-sizing: border-box;"> 查看內(nèi)存占用
接下來把List\?中的數(shù)據(jù)灌入到bitArray中看看,先上一下代碼:
public static void Main(string[] args){var rand = new Random();List<int> intCustomerIDList = Enumerable.Range(1, 5000000).OrderBy(m => rand.Next(0, 100000)).Take(5000000).ToList();BitArray bitArray = new BitArray(intCustomerIDList.Max() + 1);foreach (var customerID in intCustomerIDList){bitArray[customerID] = true;}Console.WriteLine("處理完畢...");Console.Read();}然后抓一下dump文件,用windbg看一下內(nèi)存占用。
0:000> !do 0x0000026e4d0332b8 Name: System.Collections.BitArray MethodTable: 00007ff8e7a89220 EEClass: 00007ff8e7c01bc0 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 00007ff8e7a98538 4001810 8 System.Int32[] 0 instance 0000026e5dfd9bd8 m_array 00007ff8e7a985a0 4001811 18 System.Int32 1 instance 5000001 m_length 00007ff8e7a985a0 4001812 1c System.Int32 1 instance 5000000 _version 00007ff8e7a95dd8 4001813 10 System.Object 0 instance 0000000000000000 _syncRoot 0:000> !DumpObj /d 0000026e5dfd9bd8 Name: System.Int32[] MethodTable: 00007ff8e7a98538 EEClass: 00007ff8e7c05918 Size: 625028(0x98984) bytes Array: Rank 1, Number of elements 156251, Type Int32 (Print Array)Fields:None從圖中可以看到,沒錯(cuò),就是bitArray類型,從Size中可以看到:
Size: 625028(0x98984) bytes?=>?625028/1024/1024 = 0.59M
看到?jīng)]有,這個(gè)就????????了,由最初的64M優(yōu)化到了0.6M,簡(jiǎn)直不要太爽,看到這么小的占用量,我感到枯燥而乏味,哈哈,這下并行跑幾十家不怕了,這里要提醒一下,如果客戶數(shù)少并且數(shù)字還大,就不要用bitArray啦,反而浪費(fèi)空間,當(dāng)然數(shù)據(jù)量小怎么用也無所謂。
三:總結(jié)
跑小店鋪的時(shí)候代碼怎么寫都行,數(shù)據(jù)量大了到處都是坑,你的場(chǎng)景也總有優(yōu)化的辦法~
總結(jié)
以上是生活随笔為你收集整理的用long类型让我出了次生产事故,写代码还是要小心点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Istio 中的 Sidecar 注入及
- 下一篇: 链路追踪在ERP系统中的应用实践