簡體   English   中英

Python 2.7.3 內存錯誤

[英]Python 2.7.3 memory error

我有一個使用 python 代碼的特定案例。 每次運行代碼時,RAM 內存都會增加,直到達到 1.8 GB 並崩潰。

import itertools
import csv
import pokersleuth

cards = ['2s',  '3s',   '4s',   '5s',   '6s',   '7s',   '8s',   '9s',   'Ts',   'Js',   'Qs',   'Ks',   'As',   '2h',   '3h',   '4h',   '5h',   '6h',   '7h',   '8h',   '9h',   'Th',   'Jh',   'Qh',   'Kh',   'Ah',   '2c',   '3c',   '4c',   '5c',   '6c',   '7c',   '8c',   '9c',   'Tc',   'Jc',   'Qc',   'Kc',   'Ac',   '2d',   '3d',   '4d',   '5d',   '6d',   '7d',   '8d',   '9d',   'Td',   'Jd',   'Qd',   'Kd',   'Ad']
flop = itertools.combinations(cards,3)

a1 = 'Ks' ; a2 = 'Qs'
b1 = 'Jc' ; b2 = 'Jd'

cards1 = a1+a2
cards2 = b1+b2

number = 0
n=0
m=0

for row1 in flop:
    if (row1[0] <> a1 and row1[0] <>a2 and row1[0] <>b1 and row1[0] <>b2) and (row1[1] <> a1 and row1[1] <>a2 and row1[1] <>b1 and row1[1] <>b2) and (row1[2] <> a1 and row1[2] <> a2 and row1[2] <> b1 and row1[2] <> b2):
        for row2 in cards:
            if (row2 <> a1 and row2 <> a2 and row2 <> b1 and row2 <> b2 and row2 <> row1[0] and row2 <> row1[1] and row2 <> row1[2]):
                s = pokersleuth.compute_equity(row1[0]+row1[1]+row1[2]+row2, (cards1, cards2))
                if s[0]>=0.5:
                    number +=1
                    del s[:]
                del s[:]

        print number/45.0
        number = 0
        n+=1

我對發生內存泄漏的測試尚無定論,但假設它沒有發生在 montecarlo.dll 中,我想我會嘗試multiprocessing.Pool()並將工作拆分為較小的塊,然后加載到某些進程我可以在他們開始使用過多內存之前終止:

from itertools import combinations, product, islice
from multiprocessing import Pool
from pokersleuth import compute_equity

num_procs = 4
num_jobs = 256
chunk_size = num_procs * num_jobs

join = ''.join

drawn = a1, a2, b1, b2 = 'Ks', 'Qs', 'Jc', 'Jd'
pairs = (a1 + a2, b1 + b2)

deck = (join(reversed(c)) for c in product('shcd', '23456789TJQKA'))
deck = [card for card in deck if card not in drawn]

def compute_chances(cards):
    return sum(compute_equity(cards + card, pairs)[0] >= 0.5
               for card in deck if card not in cards) / 45.0

if __name__ == '__main__':
    combis = (join(each) for each in combinations(deck, 3))
    i = 0
    while True:
        pool = Pool(processes=num_procs)
        chunk = list(islice(combis, chunk_size))
        for i, chances in enumerate(pool.imap(compute_chances, chunk), i + 1):
            print i, chances
        pool.terminate()
        if len(chunk) < chunk_size:
            break

結果與您的程序中的結果相同。

以下是任務管理器關於 17 個循環的最后 7 個循環(17296 種組合, chunk_size為 1024)的內存消耗情況:

10個循環

每個循環使用大約 400 MB,處理所有組合需要 34 分鍾。

我沒有手動輸入一副牌,而是讓計算機為我創建。 我拒絕做計算機可以做的事情。

為了盡可能減少傳遞給每個進程的數據量,我只將三張卡的每個組合發送到compute_chances()並在那里計算其他所有內容。

我不確定 montecarlo.dll 是否可重入,但結果似乎表明它是可重入的。

我的代碼中num_procsnum_jobs的值在我的機器上運行良好。 您應該使用它們來找到適合您的最佳設置。

您在 Linux 運行(我說得對嗎?),並且您達到了系統上的最大進程映像大小,因為 linux 上的進程無法減少它們的內存大小。 視窗。

您的選擇是將其拆分,以便在達到內存限制、編譯具有更高進程大小限制的內核 或在 windows 上運行 后可以恢復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM