2011-08-07 40 views
4

我已經按照這裏優良的崗位如何設置heapy使用Django:http://www.toofishes.net/blog/using-guppy-debug-django-memory-leaks/使用heapy追查在Django應用程序的內存泄漏

我吩咐hp.setref(),現在一段時間後,我也得到數據hp.heap():

>>> hp.heap() 
Partition of a set of 12075 objects. Total size = 1515496 bytes. 
Index Count %  Size % Cumulative % Kind (class/dict of class) 
    0 4048 34 339656 22 339656 22 str 
    1 3112 26 269368 18 609024 40 tuple 
    2 171 1 169992 11 779016 51 dict (no owner) 
    3 1207 10 144440 10 923456 61 list 
    4  49 0 102040 7 1025496 68 dict of module 
    5 591 5 66984 4 1092480 72 unicode 
    6 498 4 59760 4 1152240 76 function 
    7 433 4 51960 3 1204200 79 types.CodeType 
    8  57 0 50480 3 1254680 83 type 
    9  36 0 31584 2 1286264 85 dict of class 

但現在呢?我應該從這個輸出中理解什麼?我應該如何開始追蹤那些'str'和'tuple'對象屬於哪裏?

隨着get_rp,我獲得以下的輸出:

>>> hp.heap().get_rp() 
Reference Pattern by <[dict of] class>. 
0: _ --- [-] 12000 (0xd1d340 | 0xd88b50 | 0xf63f00 | __builtin__.Struct | __... 
1: a  [-] 137 dict (no owner): 0x761c30*160, 0x7655d0*1491, 0x781640*9... 
2: aa ---- [-] 45 dict of django.db.models.options.Options: 0xcf3110... 
3: a3  [-] 45 django.db.models.options.Options: 0xcf3110, 0xf0bb10... 
4: a4 ------ [-] 140 dict of django.db.models.related.RelatedObject: 0x10bec... 
5: a5   [-] 140 django.db.models.related.RelatedObject: 0xf14450... 
6: a6 -------- [-] 63 dict of django.db.models.fields.related.ForeignKey: 0x... 
7: a7   [+] 63 django.db.models.fields.related.ForeignKey: 0xf0e690... 
8: a5b ------- [-] 7 dict of django.db.models.fields.related.OneToOneField: ... 
9: a5ba   [+] 7 django.db.models.fields.related.OneToOneField: 0x15447... 

現在是正確的假設,它的Django正在泄漏內存?但那些沒有所有者的字典是什麼?

回答

0

我沒有與任何heapy經驗,但以我的經驗,Django的(和大多數其他Python程序)不泄漏內存,但是他們也沒有清理內存pristinely一些希望。

此外,Django的設置會導致它因爲診斷原因而消耗內存。例如,設置DEBUG = True可以使其保留所有SQL查詢,所以進程運行的時間越長,它使用的內存就越多。

更新:您的問題不在您的Python代碼中。看heapy給你的總結:那裏代表的內存總大小是1.5Mb!當Python程序真正泄漏時,最常見的原因是泄漏的C擴展。你有沒有在Django進程下運行的C擴展?

+1

謝謝,但這不是真的回答我的問題。這是關於如何解釋結果。我們顯然存在內存泄漏(進程耗盡2GB或mem),並且與我們的生產環境中的DEBUG = True無關,因爲它是False。 – petteri

+0

使用幾十萬個模型實例,您可以消耗大量內存部分而不會泄漏。 –

+0

@petteri:我已經更新了我的答案。 –