最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Memory limit in Python - Stack Overflow

programmeradmin2浏览0评论

My problem is that I keep getting a memory limit in python. I have three sets of code that generate combinations of a set of characters. I have run it on simulators and if it tries to generate combinations that are four characters long, it kills itself at about 1/8 the way through. I replaced the backslash character with BackslashCharacter. My python has been updated already to 64 bit but it still has some memory errors for the code. I have three peices of code. By the way i changed the settings on my computer to use 32000 to 40000 bytes(I think it was bytes) of memory. Should I also use task manager to prioritize python?

All help is greatly appreciated!

This is the first code: It prints lines that are combinations of a set of characters. It kills itself at about 1/8 the way through the process.

stuff = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]","Backslash","A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "]
len=len(stuff)
def generate(stuff,i,s,len):
    if (i == 0):
        print(s)
        return
    for j in range(0,len):
        appended = s + stuff[j]
        generate(stuff,i-1,appended,len)
    return
def crack(stuff,len):
    for i in range(18,19):
        generate(stuff,i,"",len)
crack(stuff,len)

This is the second code: It should create a file and write the combinations in a table-like format but it also kills itself at about 1/8 the way through the process. It does this for a 3 character length set of combinations. Do you know how to make the file bigger? Is there a way to allocate more memory to this?

characters = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]",'"BackslashCharacter"',"A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "]
import itertools
combinations = list(itertools.product(characters,repeat=3))
testfileofcombinationsname = "testfile.txt"
size_in_bytes=(1024*1024)
filename = "combinationsfile"
import os
def testtxtcombinationsfile(testfileofcombinationsname, size_in_bytes):
    with open({testfileofcombinationsname}, 'wb') as combinationsfile:
        {filename}.seek(size_in_bytes - 1)
        {filename}.write(b'\0')
with open(testfileofcombinationsname, 'a') as combinationsfile:
    for row in combinations:
        line = "  ".join(row)
        combinationsfile.write(line + '\n')
print('A "said" table has been appended and written to testfile.txt!')

This is my third code: It also generates a txt file and prints combinations in it. though the simulator I ran it on says that it is over 4KB considering the small number for the seek part. Do you know why this is happening? Once, it generated a file that was filled with periods highlighted in red. Any idea why this happened? Do you know why this is generating such a large file when the seek number is so small? Do you know a way to use the seek part properly in order to create a txt file of lets say 8KB in size? Is there anything that I can do to make this run more better with more memory allocated to it?

characters = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]",'"BackslashCharacter"',"A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "]
import itertools
combinations = list(itertools.product(characters,repeat=3))
testfileofcombinationsname = "testfile.txt"
combinationsfile = open("testfile.txt", "wb")
combinationsfile.seek(4)
combinationsfile.write(b"\0")
with open(testfileofcombinationsname, 'a') as combinationsfile:
    for row in combinations:
        line = "  ".join(row)
        combinationsfile.write(line + '\n')

My problem is that I keep getting a memory limit in python. I have three sets of code that generate combinations of a set of characters. I have run it on simulators and if it tries to generate combinations that are four characters long, it kills itself at about 1/8 the way through. I replaced the backslash character with BackslashCharacter. My python has been updated already to 64 bit but it still has some memory errors for the code. I have three peices of code. By the way i changed the settings on my computer to use 32000 to 40000 bytes(I think it was bytes) of memory. Should I also use task manager to prioritize python?

All help is greatly appreciated!

This is the first code: It prints lines that are combinations of a set of characters. It kills itself at about 1/8 the way through the process.

stuff = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]","Backslash","A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "]
len=len(stuff)
def generate(stuff,i,s,len):
    if (i == 0):
        print(s)
        return
    for j in range(0,len):
        appended = s + stuff[j]
        generate(stuff,i-1,appended,len)
    return
def crack(stuff,len):
    for i in range(18,19):
        generate(stuff,i,"",len)
crack(stuff,len)

This is the second code: It should create a file and write the combinations in a table-like format but it also kills itself at about 1/8 the way through the process. It does this for a 3 character length set of combinations. Do you know how to make the file bigger? Is there a way to allocate more memory to this?

characters = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]",'"BackslashCharacter"',"A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "]
import itertools
combinations = list(itertools.product(characters,repeat=3))
testfileofcombinationsname = "testfile.txt"
size_in_bytes=(1024*1024)
filename = "combinationsfile"
import os
def testtxtcombinationsfile(testfileofcombinationsname, size_in_bytes):
    with open({testfileofcombinationsname}, 'wb') as combinationsfile:
        {filename}.seek(size_in_bytes - 1)
        {filename}.write(b'\0')
with open(testfileofcombinationsname, 'a') as combinationsfile:
    for row in combinations:
        line = "  ".join(row)
        combinationsfile.write(line + '\n')
print('A "said" table has been appended and written to testfile.txt!')

This is my third code: It also generates a txt file and prints combinations in it. though the simulator I ran it on says that it is over 4KB considering the small number for the seek part. Do you know why this is happening? Once, it generated a file that was filled with periods highlighted in red. Any idea why this happened? Do you know why this is generating such a large file when the seek number is so small? Do you know a way to use the seek part properly in order to create a txt file of lets say 8KB in size? Is there anything that I can do to make this run more better with more memory allocated to it?

characters = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]",'"BackslashCharacter"',"A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "]
import itertools
combinations = list(itertools.product(characters,repeat=3))
testfileofcombinationsname = "testfile.txt"
combinationsfile = open("testfile.txt", "wb")
combinationsfile.seek(4)
combinationsfile.write(b"\0")
with open(testfileofcombinationsname, 'a') as combinationsfile:
    for row in combinations:
        line = "  ".join(row)
        combinationsfile.write(line + '\n')
Share Improve this question asked Mar 17 at 13:06 Python LearnerPython Learner 71 bronze badge 5
  • 1 It is reasonably clear that you are creating too many recursions within the code. But the main question I would ask is, what exactly is your code trying to do? – OldBoy Commented Mar 17 at 13:43
  • Don't convert the return value from itertools.product to a list. That's almost certainly where you're exceeding your memory limit. Just iterate over the returned itertools.product class and write to your file one item at a time – Adon Bilivit Commented Mar 17 at 13:49
  • 1 Why are you setting such a tiny memory limit? – no comment Commented Mar 17 at 13:52
  • 1 "My problem is that I keep getting a memory limit in python." what does that mean exactly? What exactly are you encountering? " but it still has some memory errors for the code" what errors? – juanpa.arrivillaga Commented Mar 17 at 14:57
  • 2 What is the point of for i in range(18,19)? Where are you limiting things to "combinations that are four characters long" – JonSG Commented Mar 17 at 17:31
Add a comment  | 

1 Answer 1

Reset to default 1

The combinations of just punctuation, ascii characters, and digits is about 100 (it is a little less but 100 makes the math easier). So, passwords of length 4 result in a 10 million line file of 4 character strings or something shy of 400meg. Your final attempt is on track to generate such a file. This code uses password length of 3. Increase at your own peril.

import string
import itertools

## ---------------------
## Waring about resulting file sizes:
##  1 == 1k
##  2 == 26k
##  3 == 3.2m
##  4 == 300m?
##  5 == 30g?
## ---------------------
password_length = 3
possible_characters = string.ascii_letters + string.digits + string.punctuation
## ---------------------

with open("passwords.txt", "w") as file_out:
    for attempt in itertools.product(possible_characters, repeat=password_length):
        file_out.write("".join(attempt) + "\n")

The upshot of this is that you rapidly see why cracking passwords through brute force is a problem for well formed passwords.

发布评论

评论列表(0)

  1. 暂无评论