I've recently finished writing a genetic algorithm. The algo creates a list of 50 test cases, and runs that trial through a simulation, and stores the result. I use a number of custom classes, and the stored results contain lists of class instances.
When I run this simulation, beyond taking forever, it runs my Memory Usage up to 90+%. There must be somewhere in my code that is just hogging memory, but I'm not sure how to find it. Here is the main part of my code, with the loops inside loops inside loops.....
# ----------------------------------------------------------------------------------------------------
for i in range(1,numberOfRunsToSolve,1): # begin genetic algo loop
temp = trial_cases
for ii, stored_trial in enumerate(temp): # run through stored trial cases
new_trials = []
for jj in range(1,numberOfTrialsPerRound):
tc = []
tc = randomTrials_GenAlgo_ARRAY(stored_trial, True) # create new trial set, based on indexed stored results
new_trials.append(tc)
print(new_trials)
for j, trial in enumerate(new_trials):
x = OneGenerationSimulation(trial) #returns [ObjArray, ErrorArray]
rev = revenueAndLoss(x[0])
DEBUG_ARRAY.append(str(revenue)+' trial: ' + str(trial))
results.append((revenue, trial, x[0],x[1]))
results.sort(reverse=True) # sort to bring best revenue to top
results = results[0:numberOfResultsToKeepPerRound-1]
trial_cases = []
for i, r in enumerate(results):
trial_cases.append(r[1])
# end of the genetic algo loop
# ----------------------------------------------------------------------------------------------------
Any suggestions how to track memory usage in my script, and hunt down culprits? I'm pretty new to Python, so feel free to state the obvious.
EDIT: The process above is essentially doing this:
1) Create 50 trial runs.
2) Run a simulation on each trial. This simulation creates hundreds of custom objects, and runs a script on them, returning the results.
3) With all the results that come back, retrieve the best 5 outcomes.
4) With those 5 outcomes, create new trial sets, and repeat the process over again.
I'm worried that the mass-object instance creation, which is then filtered down to the best 5 results, isn't being cleaned up properly in memory or something... and that all those objects are hiding in the background....
Thanks - KC.