Python 3 - Limiting Memory Usage from a Script
Question: I am using the itertools module to create a list of possible permutations for
the order of letters in a rather long sentence. However, every time I do so I
run out of memory (I have 16GB RAM before anyone asks).

I don't have the code on this machine, however it is not inefficient code
since it is a carbon copy of one of the examples in the documentation, there
are simply too many permutations which Python is trying to do all at once.

The questions is, is there a way of limiting the amount of memory that Python
uses, maybe by giving it a pool? I know I should probably change the code, but
I would benefit from a pool of memory for other projects as well.

I cannot use the Theano module because I am using Conda, which is
incompatible. I have tried the gc module with little effect, but again, the
code is an example, and the sentence is about a dozen characters, printing the
list on screen.

**Edit:**

Here's the main section of my code. _I do not suggest running it_ since it
causes my machine to crash.

    
    
    import itertools
    f = open('File.txt','w')
    for key, value in dict.items():
        print(list(itertools.permutations((str(counter-value)))),file=f)
    

The dict variable is a 76 element dictionary containing different characters
which the code checks. The actual function of the code is complicated and fits
into a hundred or so line script, but this is the point that I'm having
problems with. If the code works, it should be calculation literally millions
of permutations. My problem is that it tries to do them all at once. I want to
know if there is some way I can limit it, even if it means the code will run
slower.


Answer: You can just loop over the permutations and write each of them to the file,
like this:

    
    
    import itertools
    f = open('File.txt','w')
    for key, value in dict.items():
        for i in itertools.permutations((str(counter-value))):
            print(i, file=f)
    

As it is a generator, the items are retrieved one-by-one so your memory won't
be exhausted.

