473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# memory consumption outta control...

4
Hello Gentlemen..

I have a program that is calculating all possible permutations of objects within an object.


The code looks very much like...
Expand|Select|Wrap|Line Numbers
  1. foreach (item as currentItem in Items)
  2.   {
  3.      MasterObject.Item1 = currentItem;
  4.     foreach (item2 as currentItem2 in Items2)
  5.      {
  6.        MasterObject.Item2 = currentItem2;
  7.       foreach (item3 as currentItem3 in Items3)
  8.        {
  9.          MasterObject.Item3 = currentItem3;     
  10.          foreach (item4 as currentItem4 in Items4)
  11.          {
  12.  
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.
Feb 1 '09 #1
8 3664
Plater
7,872 Expert 4TB
Well I'm not clear on every aspect, but maybe it's not getting a chance to run garbage collection?
Somewhere in your nesting add a GC.Collect() and see if that effects the memory consumption?
Feb 2 '09 #2
SageX
4
Hey thanks for the response Plater. I will give that a shot and let you guys know if it resolves the issue.
Feb 2 '09 #3
vekipeki
229 Expert 100+
Note that neither GC nor you can free the memory, if it is actually being used.

What I am trying to say is: it looks like you have 4 collections of items, which point to other objects. When you assign an item to a property (MasterObject.Item1 = currentItem), you are not creating a new instance or your currentItem, so this is not where your memory starts to grow.

So, if your memory consumption is low before beginning of this code, it means that you are allocating lots of stuff somewhere else inside the nested code?
Feb 3 '09 #4
r035198x
13,262 8TB
20 levels of foreach nesting doesn't sound clean. Perhaps you can refactor your code (perhaps by creating some classes here and there) and tidying up the design a bit?
Feb 3 '09 #5
SageX
4
Hello Everyone,

First of all I would like to say thank you for your posts, and apologize for not responding more promptly. Unfortunately this was a program which was not high priority and it took me a while to find the solution.

What was happening is this, I was storing objects for successful hits to certain criteria out of a high growth permutation. What happened is that I was storing all possible matches, then sorting through them at the end and determining which results *best* met other sets of criteria.

In truth, out of hundreds of billions of permutations I only expected a few thousand in the final match collection.

What did happen is a huge amount of matches being returned and stored. In this case, I was seeing the memory grow because of the sheer number of successful matches.

Once I discovered this, I simply moved my end filtering process to the center of the loop, and there was no longer a reason to store and sort all that data anymore. It sped up the routine greatly and now the footprint is very small.

Again, thanks for everyone's help.
Feb 11 '09 #6
SageX
4
@r035198x
Hey man, thanks for the reply. I am all about refactoring.

The nested foreach loops were a mechanism to derive all possible permutations of objects out of a set.

Think of it as...

SLOT1 (Item1,Item2,Item3,Item4,Item5)
SLOT2(Item6,Item7,Item8,Item9,Item10)
SLOT3(Item11,Item12,Item13,Item14,Item15)

out to 20 slots...

And you want to get every possible permutation of Item1 in SLOT1, with Item6 in SLOT2, but also Item7 in SLOT2, then go back and iterate SLOT1 to Item2 and then go back to SLOT2, Item6, etc..)

Just like counting binary...:)

Is there any way could could think of a more efficient way? I am sure there is some formula I could implement that would speed the process, some type of statistics similar to PPM or CTW...??
Feb 11 '09 #7
r035198x
13,262 8TB
I'll stick it on my to do list and have a look later today after I've had my coffee. In the meantime you can PM the Java village idiot JosAH and ask him to have a look at this thread. His eyes light up every time he senses a combinatorial problem.
Feb 11 '09 #8
vekipeki
229 Expert 100+
It depends on you "filtering process", as you called it - i.e. how do you decide if a permutation should be discarded or not, and how early can you make that decision?

For example: someone told you to print all positive numbers smaller than 1,000,000 which start with a specified digit (0 to 9). A naive approach would be to count from 0 to 1,000,000 and make a check for each number separately. This would mean you have to make a check 1,000,000 times.

But if you rethink, you can realize that, if for example the specified digit is "4", those numbers can only be 4, 40-49, 400-499, ..., 400000-499999.

In that case your outer loop would only count the number of digits (from 1 to 6 in this case), and in each iteration you would generate your bounds based on the known criteria.

So, if you really must create all permutations to check them one by one, then there is no way to optimize it.
Feb 11 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: sbayeta | last post by:
Hi, I'd like to know who is responsible of memory recycling and defragmentation in a C/C++ program, assuming all the memory allocation/deallocation is done using malloc/free or new/delete. ...
5
by: Sharon | last post by:
I’m writing a Windows application. In the form I have a Panel and inside the panel I have a PictureBox control. I’m loading the PictureBox control with BMP image that has the following...
6
by: Andy | last post by:
Along with many others I've noticed the large amount of memory that can be taken up by the aspnet_wp.exe. I've found that I can better control and limit this memory consumption by including a...
8
by: nautonnier | last post by:
I know my problem has been discussed ad nauseum but I still don't get it so please bear with me. I have written a service which performs some work against a database once a day (usually in the...
12
by: Tony | last post by:
Hi expert, I installed DB2 v8.2 server on Solaris 9 box. When I connect to DB2 using control centre or other applications(except command line), around 12 db2sysc processes pop up and each one...
1
by: buu | last post by:
It's strange to me, but, create a dictionary and fill it with 1 mil. of some objects. then, see the memory consumption (arised, of course). then, clean the dictionary.... memory consumption is...
2
by: Jonas Maurus | last post by:
Hello everybody, I'm pondering the following problem: I want to write a Python program that receives messages via SMTP and stores them in a dict or an array. For my purposes it would be...
17
by: Cesar | last post by:
Hello people. I'm having a Winform app that contains a webbrowser control that keeps navigating from one page to another permanentrly to make some tests. The problem I'm having is that after a...
11
by: dhtml | last post by:
(originally mis-posted on m.p.s.jscript...) I've just closed all windows in Firefox and its using 244MB of memory. I have no idea why. I had GMail open, a page from unicode, the CLJ FAQ. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.