Honestly, it might be preferable to design your script to take a command-line parameter, e.g. -m integerG, which specifies at runtime the amount of memory that you want the program to take as its maximum.
In addition to being a convenient cop-out ... ;-) ... this simple-minded-sounding approach actually adds flexibility: If, say, at some point you want to run four instances of your program concurrently on a 16-gig machine, you would want to tell each of them, say, -m 3G. Absent the parameter, the program would assume a fixed limit of your arbitrary choosing. (“Assume that you can have it all” might or might not be a great assumption to make or to have made, down the line, even if it looks prescient now.) Instead of making a guess that might turn out to be wrong, using logic that might have to be programmatically tweaked at an inconvenient wee hour of the early morning moment, it would make a conservative assumption that you know would be okay.