使用Python的内存转储

2024-05-11 11:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python为自己编写了一个小程序,帮助我从一组不同的数字和单词中生成密码的所有组合,我知道要恢复忘记的密码,因为我知道我使用的所有不同的单词和数字集,我只想生成所有可能的组合,唯一的问题是这个列表似乎持续了好几个小时,所以最终我的内存用完了,而且没有完成。

我被告知它需要抛开我的记忆,这样才能继续下去,但我不确定这是否正确。我有办法解决这个问题吗?

这是我正在运行的程序:

#!/usr/bin/python
import itertools
gfname = "name"
tendig = "1234567890"
sixteendig = "1111111111111111"
housenum = "99"
Characterset1 = "&&&&"
Characterset2 = "££££"
daughternam = "dname"
daughtyear = "1900"
phonenum1 = "055522233"
phonenum2 = "3333333"





mylist = [gfname, tendig, sixteendig, housenum, Characterset1,
          Characterset2, daughternam, daughtyear, phonenum1, phonenum2]
for length in range(1, len(mylist)+1):
    for item in itertools.permutations(mylist, length):
            print "".join(item)

我已经拿出了几套,并改变了数字和文字明显的原因,但这是大致的程序。

另一件事是我可能遗漏了一个特定的单词,但我不想把它放在列表中,因为我知道它可能在所有生成的密码之前,有人知道如何在我的程序中添加前缀。

很抱歉语法不好,谢谢你的帮助。


Tags: 程序密码列表数字单词itertoolsmylisttendig
3条回答

我使用guppy来了解内存使用情况,我稍微更改了操作代码(标记为“!!!”)

import itertools
gfname = "name"
tendig = "1234567890"
sixteendig = "1111111111111111"
housenum = "99"
Characterset1 = "&&&&"
Characterset2 = u"££££"
daughternam = "dname"
daughtyear = "1900"
phonenum1 = "055522233"
phonenum2 = "3333333"

from guppy import hpy # !!!
h=hpy()               # !!!
mylist = [gfname, tendig, sixteendig, housenum, Characterset1,
          Characterset2, daughternam, daughtyear, phonenum1, phonenum2]
for length in range(1, len(mylist)+1):
    print h.heap() #!!!
    for item in itertools.permutations(mylist, length):
            print item # !!!

每次调用h.heap()时,Guppy都会输出类似的内容。

Partition of a set of 25914 objects. Total size = 3370200 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  11748  45   985544  29    985544  29 str
     1   5858  23   472376  14   1457920  43 tuple
     2    323   1   253640   8   1711560  51 dict (no owner)
     3     67   0   213064   6   1924624  57 dict of module
     4    199   1   210856   6   2135480  63 dict of type
     5   1630   6   208640   6   2344120  70 types.CodeType
     6   1593   6   191160   6   2535280  75 function
     7    199   1   177008   5   2712288  80 type
     8    124   0   135328   4   2847616  84 dict of class
     9   1045   4    83600   2   2931216  87 __builtin__.wrapper_descriptor

运行python code.py > code.log,显示fgrep Partition code.log

Partition of a set of 25914 objects. Total size = 3370200 bytes.
Partition of a set of 25924 objects. Total size = 3355832 bytes.
Partition of a set of 25924 objects. Total size = 3355728 bytes.
Partition of a set of 25924 objects. Total size = 3372568 bytes.
Partition of a set of 25924 objects. Total size = 3372736 bytes.
Partition of a set of 25924 objects. Total size = 3355752 bytes.
Partition of a set of 25924 objects. Total size = 3372592 bytes.
Partition of a set of 25924 objects. Total size = 3372760 bytes.
Partition of a set of 25924 objects. Total size = 3355776 bytes.
Partition of a set of 25924 objects. Total size = 3372616 bytes.

我相信这表明内存占用保持相当一致。

当然,我可能误解了guppy的结果。尽管在我的测试中,我故意在列表中添加了一个新的字符串,以查看对象计数是否增加了。

对于那些感兴趣的人,我不得不在OSX上安装guppy-Mountain Lion pip install https://guppy-pe.svn.sourceforge.net/svnroot/guppy-pe/trunk/guppy

总之,我不认为这是内存不足的问题,尽管我们没有使用完整的操作数据集。

正如你现在所知道的,你的程序本身运行起来会相当有效率。但要确保你不只是在空闲状态下运行它,例如,当空闲状态用越来越多的行更新屏幕时,这会使它慢下来,进入爬行状态。将输出直接保存到文件中。

更妙的是:你有没有想过当你有密码的时候会怎么做?如果可以从命令行登录到丢失的帐户,请尝试立即登录,而不要存储所有密码供以后使用:

for length in range(1, len(mylist)+1):
    for item in itertools.permutations(mylist, length):
        password = "".join(item)
        try_to_logon(command, password)

使用IronPython和Visual Studio作为调试工具(它们非常好)怎么样?您应该能够暂停执行并查看内存(本质上是内存转储)。

相关问题 更多 >