Hello Gentlemen..
I have a program that is calculating all possible permutations of objects within an object.
The code looks very much like...
And so forth...I have nested this 20 levels deep, to find all possible permutations.
With about 30 objects spread through item1, item2, etc..about 250MB of RAM. If I add another object, especially in the lower levels of the foreachit might swell the memory usage from 250MB to 300MB.
However, if I add an extra item to Items2, it cascades down the foreach and exponentially increases the permutations. When that occurs it spikes well over a GB of memory. The problem is, I need to add many more objects to Items1, Items2, etc...
I thought C# would be intelligent enough to manage this memory usage, instead its like it is trying to keep all possible permutations in RAM as it processes the foreach nest.
Is there anything I can do to stop this? Or is there a more efficient mechanism to search through those permutations?
At the bottom of the foreach nest, if the masterObject matches certain criteria, I am doing a deep copy to a MatchObjects arraylist.
If it does not match, I am using the same MasterObject, so its not like I am creating a new master object each time through...
Any solutions or ideas would be greatly appreciated.
I have a program that is calculating all possible permutations of objects within an object.
The code looks very much like...
Code:
foreach (item as currentItem in Items)
{
MasterObject.Item1 = currentItem;
foreach (item2 as currentItem2 in Items2)
{
MasterObject.Item2 = currentItem2;
foreach (item3 as currentItem3 in Items3)
{
MasterObject.Item3 = currentItem3;
foreach (item4 as currentItem4 in Items4)
{
With about 30 objects spread through item1, item2, etc..about 250MB of RAM. If I add another object, especially in the lower levels of the foreachit might swell the memory usage from 250MB to 300MB.
However, if I add an extra item to Items2, it cascades down the foreach and exponentially increases the permutations. When that occurs it spikes well over a GB of memory. The problem is, I need to add many more objects to Items1, Items2, etc...
I thought C# would be intelligent enough to manage this memory usage, instead its like it is trying to keep all possible permutations in RAM as it processes the foreach nest.
Is there anything I can do to stop this? Or is there a more efficient mechanism to search through those permutations?
At the bottom of the foreach nest, if the masterObject matches certain criteria, I am doing a deep copy to a MatchObjects arraylist.
If it does not match, I am using the same MasterObject, so its not like I am creating a new master object each time through...
Any solutions or ideas would be greatly appreciated.
Comment