Python中的內(nèi)存管理之python list內(nèi)存使用詳解
使用 Python 的時(shí)候,我們知道 list 是一個(gè)長(zhǎng)度可變對(duì)的數(shù)組, 可以通過(guò) insert,append 和 extend 輕易的拓展其中的元素個(gè)數(shù)。 也可以使用運(yùn)算符 如: [1] + [2] 生成新的數(shù)組[1, 2]
extend()、"+"、"+="的區(qū)別
- "+"將兩個(gè) list 相加,會(huì)返回到一個(gè)新的 list 對(duì)象
- append 在原 list 上進(jìn)行修改,沒(méi)有返回值
從以下代碼可以看到, 調(diào)用 b = b + [3, 4] 之后, 通過(guò)id(b) 查看 b 變成了一個(gè)新對(duì)象。
In [5]: b = [1, 2] In [6]: id(b) Out[6]: 1628740249224 In [7]: b = b + [3, 4] In [8]: id(b) Out[8]: 1628740456520
使用extend() 完成相同的步驟, 可以看到 對(duì)象c 的id保持和原來(lái)的一致
In [9]: c = [1, 2] In [10]: id(c) Out[10]: 1628740392584 In [11]: c.extend([3, 4]) In [12]: id(c) Out[12]: 1628740392584
使用 "+=" 連接列表, 看到效果和 extend() 是相同的。
In [1]: a = [1, 2] In [2]: id(a) Out[2]: 1628740021448 In [3]: a += [3, 4] In [4]: id(a) Out[4]: 1628740021448
結(jié)論: 減少內(nèi)存的拷貝, 修改一個(gè)列表的數(shù)據(jù)時(shí), 應(yīng)避免使用 list1= list1+ list2 這樣的語(yǔ)法。
List的內(nèi)存使用
一個(gè)示例:
In [1]: import sys In [2]: lst1 = [1] In [3]: lst2 = [] In [4]: lst2.append(1) In [5]: lst1 == lst2 Out[5]: True In [6]: sys.getsizeof(lst1) Out[6]: 72 In [7]: sys.getsizeof(lst2) Out[7]: 96
可以看到,lst1 == lst2, 但是當(dāng)使用 sys.getsizeof 獲取對(duì)象的內(nèi)存大小時(shí), 兩者卻是不同的。
如下圖所示, list_a 長(zhǎng)度為4, 當(dāng)執(zhí)行 append(4) 時(shí), 底層的數(shù)據(jù)長(zhǎng)度其實(shí)申請(qǐng)了4個(gè)元素的空間,當(dāng)再次執(zhí)行 append(5) 的時(shí)候,不需要再次申請(qǐng)內(nèi)存。
因?yàn)?執(zhí)行 append() 操作時(shí),Python將一次拓展N個(gè)元素的內(nèi)存,因?yàn)橐粋€(gè) append 操作很可能是很多 append 操作的開(kāi)始,通過(guò)額外分配內(nèi)存來(lái)減少可能的內(nèi)存分配和內(nèi)存copy的次數(shù)。
In [1]: import sys
In [2]: l = []
...: print(f'list initial size {sys.getsizeof(l)}')
...: for i in range(80):
...: cur_size = sys.getsizeof(l)
...: l.append(i)
...: new_size = sys.getsizeof(l)
...: print(f'list len {i+1}:\t current_size {new_size}\t new_allocated 8 * {(new_size-cur_size)/8}')
...:
list initial size 64
list len 1: current_size 96 new_allocated 8 * 4.0
list len 2: current_size 96 new_allocated 8 * 0.0
list len 3: current_size 96 new_allocated 8 * 0.0
list len 4: current_size 96 new_allocated 8 * 0.0
list len 5: current_size 128 new_allocated 8 * 4.0
list len 6: current_size 128 new_allocated 8 * 0.0
list len 7: current_size 128 new_allocated 8 * 0.0
list len 8: current_size 128 new_allocated 8 * 0.0
list len 9: current_size 192 new_allocated 8 * 8.0
list len 10: current_size 192 new_allocated 8 * 0.0
list len 11: current_size 192 new_allocated 8 * 0.0
list len 12: current_size 192 new_allocated 8 * 0.0
list len 13: current_size 192 new_allocated 8 * 0.0
list len 14: current_size 192 new_allocated 8 * 0.0
list len 15: current_size 192 new_allocated 8 * 0.0
list len 16: current_size 192 new_allocated 8 * 0.0
list len 17: current_size 264 new_allocated 8 * 9.0
list len 18: current_size 264 new_allocated 8 * 0.0
list len 19: current_size 264 new_allocated 8 * 0.0
list len 20: current_size 264 new_allocated 8 * 0.0
list len 21: current_size 264 new_allocated 8 * 0.0
list len 22: current_size 264 new_allocated 8 * 0.0
list len 23: current_size 264 new_allocated 8 * 0.0
list len 24: current_size 264 new_allocated 8 * 0.0
list len 25: current_size 264 new_allocated 8 * 0.0
list len 26: current_size 344 new_allocated 8 * 10.0
list len 27: current_size 344 new_allocated 8 * 0.0
list len 28: current_size 344 new_allocated 8 * 0.0
list len 29: current_size 344 new_allocated 8 * 0.0
list len 30: current_size 344 new_allocated 8 * 0.0
list len 31: current_size 344 new_allocated 8 * 0.0
list len 32: current_size 344 new_allocated 8 * 0.0
list len 33: current_size 344 new_allocated 8 * 0.0
list len 34: current_size 344 new_allocated 8 * 0.0
list len 35: current_size 344 new_allocated 8 * 0.0
list len 36: current_size 432 new_allocated 8 * 11.0
list len 37: current_size 432 new_allocated 8 * 0.0
list len 38: current_size 432 new_allocated 8 * 0.0
list len 39: current_size 432 new_allocated 8 * 0.0
list len 40: current_size 432 new_allocated 8 * 0.0
list len 41: current_size 432 new_allocated 8 * 0.0
list len 42: current_size 432 new_allocated 8 * 0.0
list len 43: current_size 432 new_allocated 8 * 0.0
list len 44: current_size 432 new_allocated 8 * 0.0
list len 45: current_size 432 new_allocated 8 * 0.0
list len 46: current_size 432 new_allocated 8 * 0.0
list len 47: current_size 528 new_allocated 8 * 12.0
list len 48: current_size 528 new_allocated 8 * 0.0
list len 49: current_size 528 new_allocated 8 * 0.0
list len 50: current_size 528 new_allocated 8 * 0.0
list len 51: current_size 528 new_allocated 8 * 0.0
list len 52: current_size 528 new_allocated 8 * 0.0
list len 53: current_size 528 new_allocated 8 * 0.0
list len 54: current_size 528 new_allocated 8 * 0.0
list len 55: current_size 528 new_allocated 8 * 0.0
list len 56: current_size 528 new_allocated 8 * 0.0
list len 57: current_size 528 new_allocated 8 * 0.0
list len 58: current_size 528 new_allocated 8 * 0.0
list len 59: current_size 640 new_allocated 8 * 14.0
list len 60: current_size 640 new_allocated 8 * 0.0
list len 61: current_size 640 new_allocated 8 * 0.0
list len 62: current_size 640 new_allocated 8 * 0.0
list len 63: current_size 640 new_allocated 8 * 0.0
list len 64: current_size 640 new_allocated 8 * 0.0
list len 65: current_size 640 new_allocated 8 * 0.0
list len 66: current_size 640 new_allocated 8 * 0.0
list len 67: current_size 640 new_allocated 8 * 0.0
list len 68: current_size 640 new_allocated 8 * 0.0
list len 69: current_size 640 new_allocated 8 * 0.0
list len 70: current_size 640 new_allocated 8 * 0.0
list len 71: current_size 640 new_allocated 8 * 0.0
list len 72: current_size 640 new_allocated 8 * 0.0
list len 73: current_size 768 new_allocated 8 * 16.0
list len 74: current_size 768 new_allocated 8 * 0.0
list len 75: current_size 768 new_allocated 8 * 0.0
list len 76: current_size 768 new_allocated 8 * 0.0
list len 77: current_size 768 new_allocated 8 * 0.0
list len 78: current_size 768 new_allocated 8 * 0.0
list len 79: current_size 768 new_allocated 8 * 0.0
list len 80: current_size 768 new_allocated 8 * 0.0
通過(guò)觀察可以發(fā)現(xiàn), 列表從0 增加到 80長(zhǎng)度的過(guò)程中, 新申請(qǐng)的內(nèi)存長(zhǎng)度為 [4, 4, 8, 9, 10, 11, 12, 13, 14, 16] 。 反之, 當(dāng)執(zhí)行 remove 或者 pop 減少列表中的數(shù)據(jù)時(shí), 列表也會(huì)自動(dòng)縮容。
- 擴(kuò)容條件,新長(zhǎng)度大于底層數(shù)組長(zhǎng)度;
- 縮容條件,新長(zhǎng)度小于底層數(shù)組長(zhǎng)度的一半;
結(jié)論: 避免使用類(lèi)似 append 語(yǔ)法初始化列表, 優(yōu)先使用列表表達(dá)式
# Bad ❌ list_a = [] for i in range(50): list_a.append(i) # Good ✔️ list_b = [i for i in range(50)]
結(jié)論:
① 避免使用 "+" 修改數(shù)組
② 盡量避免多次使用 append 函數(shù)
到此這篇關(guān)于Python中的內(nèi)存管理之python list內(nèi)存使用詳解的文章就介紹到這了,更多相關(guān)python list內(nèi)存使用內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。