ArrayList() of Class or Struct. Best memory usage

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I use C# on Compact Framework for PPC program development. Well, I need
to use many ArrayList() of data and I don't know which is the best memory
management that I will have to use. Is more convenient to use a Class() or a
Struct() to store my data? For example (with some sintax errorrs):

class TPoint
{
int x;
int y;
}
or
struct TPoint
{
int x;
int y;
}

ArrayList MyPoints=new ArrayList();
PPoint=new TPoint();
for (iLoop=1; iLoop<=1000; iLoop++)
{
TPoint PPoint=(TPoint) MyPoints[MyPoints.Add(new TPoint())];
PPoint.x=iLoop;
PPoint.y=iLoop;
}

I read that Class() are stored in the Heap, while Struct() are stored in the
Stack memory. Well, which is the best way (for memory space, memory
performance, etc...) to store my data in PPC memory where I have a little
memory to manage?
 
Depends on the size of struct but for the example you gave structs will be
more efficient memory wise (some suggest under 12-16 bytes go for structs
and anything more use classes). However you will incur a boxing hit if you
use value types in an ArrayList so only by measuring the real scenario
yourself can you come up with an answer. If you know how many you'll have
beforehand, struct with normal arrays is the best solution in my view (until
we get generics in the next version).

Cheers
Daniel
 
Back
Top