GPWiki.org
GPWiki.org
It is currently Thu Jun 20, 2013 5:57 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: C# memory management
PostPosted: Thu Jun 05, 2008 2:11 am 
Hey, I hope I'm asking this in the right forum. Anyway, I'm somewhat new to c#, but I've done a lot of programming in C++ and I'm familiar with .NET. I'm working on a program that needs to rapidly recalculate and reset many variables in arrays. Now at first I was going to do it like I would with a C++ program and have a pointer array defined like this (I'll use integers for the example):

Code:
int numbers*;

And then I'd have it combined with a counter to keep track of the number of elements like this:
Code:
int numbers_count;

Then when I need to dynamically add something to the list I'd do it this way
Code:
int temp* = new int[numbers_count];
temp = numbers;
delete [] numbers;

numbers_count ++;
numbers = new int[numbers_count];
numbers[numbers_count-1] = /*the new value*/;

for (int i=0; i<numbers_count-1; i++) {
    numbers[i] = temp[i];
}
delete [] temp;


Ok so that was probably a bit verbose, but I think it gets the idea of what I was trying to do across. The function to remove a member from "numbers" was similar. I put this whole thing into C# and the only parts that didn't work were the delete functions and the pointers. So I changed them to regular integers and took out the delete statements. I looked up some help that said that C# has automatic memory handling, so I figured it'd be fine. But when I run this type of code where variables are re-assigned with the "new" statement frequently, the program starts running fast and then drastically slows down over time. It is quickly filling memory that it doesn't need and using more CPU than it should. Normally if I were using c++, I'd say that it was allocating a ton of memory slots and then not deleting them when finished with them. This would also cause problematic memory leaks without the delete statements.

So what I want to know is if there is another, possibly easier, way to do what I described above without filling up ridiculous amounts of memory with useless variables.


Top
  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 3:24 am 
King Code Monkey
User avatar

Joined: Wed Sep 01, 2004 3:05 pm
Posts: 11200
Location: Abingdon, MD
What about something like:

List<int> list = new List<int>();

list.Add(some_new_amount);

What else did you need to do?

_________________
Bored? Head on over to my blog and see what I'm up to.

Microsoft XNA MVP


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 3:32 am 
Dexterous Droid

Joined: Fri Aug 19, 2005 10:34 am
Posts: 3650
I'm not exactly sure at what you're doing. From the code you posted, are you trying to resize an array to give it one more element, then set the value of that new element? If so, Array.Resize() will resize the array for you. Or you can use a List like Machaira suggested. If you are changing this array frequently, the List will probably be best since it will overallocate, preventing so many reallocations. If memory is a concern, you can always use List.Trim, which will remove the over-allocated elements.

_________________
NetGore - Open source online RPG engine


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 4:27 am 
P2k
User avatar

Joined: Tue Aug 23, 2005 5:11 am
Posts: 2145
The problem is that as the array grows you are copying and allocating more and more memory every time you add an item. As the arrays get really big they go in the large object heap and then your performance plummets because you really aren't supposed to allocate memory that way.

You best bet is to use List<int> like Machaira said. Or if you really want to implement your own, then double the size of the array every time you have to enlarge it (this will drastically reduce your memory usage)

In general, you are going to have problems if you take C++ code and try to convert it line for line into .NET.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 4:52 am 
Ok thanks for the help guys. I haven't gotten a chance to try it yet, but I'll try out the List<> class and post back with my results.


Top
  
 
 Post subject: Re: C# memory management
PostPosted: Fri Aug 05, 2011 1:37 am 
Hi guys. I would have to agree with using the generic C# list. It is probably implemented by Red-Black tree (I would guess at least? I could be mistaken) or a fancy linked list. Either way, inserts and deletes through Add() and Remove() will be cheaper than dynamically resizing.

Bryan


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group