使用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)
为了明显的原因,我删掉了一些数字和单词的组合,但大致上就是这个程序。
还有一个问题是,我可能漏掉了一个特定的单词,但我不想把它放进列表里,因为我知道它可能会在所有生成的密码之前。有没有人知道怎么给我的程序添加一个前缀?
抱歉语法不好,感谢任何提供的帮助。
4 个回答
你的程序现在运行得很高效,但要注意不要只在IDLE里运行它。因为在IDLE中运行时,屏幕会不断更新显示更多的内容,这样会让程序变得非常慢。最好是把输出直接保存到一个文件里。
更进一步:你有没有想过当你拿到密码后该怎么做?如果你可以通过命令行直接登录到那个丢失的账户,建议你立即去做,而不是先把所有密码存起来,等以后再用:
for length in range(1, len(mylist)+1):
for item in itertools.permutations(mylist, length):
password = "".join(item)
try_to_logon(command, password)
为了回答@shaun的评论,如果你想把文件的输出结果保存到记事本里,可以这样运行你的文件:
Myfile.py >output.txt
如果这个文本文件还不存在,它会自动创建一个。
编辑:
把你代码底部的那一行:
print "" .join(item)
替换成这个:
with open ("output.txt","a") as f:
f.write('\n'.join(items))
f.close
这样就会生成一个叫做output.txt的文件。应该可以用(我还没测试过)。
我使用了 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 - Mountain Lion 上安装 guppy 是这样做的:pip install https://guppy-pe.svn.sourceforge.net/svnroot/guppy-pe/trunk/guppy
总的来说,我认为这并不是内存不足的问题,尽管我们没有使用完整的原始数据集。