Class Memory Usage

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi,

I have a class like the following:

public class Record
{
public double lastBasedOn;
public double total;
public double totalCount;
public double[] sum;
public long[] count;
public double[,] max;
public double[,] min;
}

My problem is that I don't know what variables I am going to use until
run time, i.e. I may only need to keep record of total and sum. Would
this then mean that the lastBasedOn & totalCount variables would
require memory space even though I won't use them? Memory usage us
important because there could be thousands of instances of the Record
class created.
Also, I use double and long above as I don't know their maximum
potential values until run time.

So I guess what i'm asking, is can I create c# classes dynamically
i.e. create:

public class Record
{
public double total;
public double[] sum;
}

Any ideas?

thanks,
 
Jazzper said:
I think if you declare the variables as their Object types rather
than their value types you will save the memory space.
No.

C# allows you
to declare an integer as a value type e.g.:

int iCount

or as it's corresponding object e.g.

Integer iCount = new Integer()

There *is* no Integer type. You seem to be thinking of Java.
I'm pretty sure If you use the Object then it will not take up memory
until you instantiate it. You just have to remember to instantiate
the necessary objects before you use them. So your class would look
like this:

The variable will still take up memory though - it'll just have a
default value of null.

In the case of doubles, I suppose you save *some* space by declaring
variables as objects instead of doubles - but only if the *vast*
majority of your variables will never be assigned a non-null value, or
if you're sharing a lot of boxed values. Performance will also go
through the floor when you try to use the values, as you'll go through
boxing and unboxing all the time. In short, it's almost always a really
bad idea.
 
Jazzper said:
I think if you declare the variables as their Object types rather than
their value types you will save the memory space. C# allows you to declare
an integer as a value type e.g.:

Yes & No (Mostly no)

For value types (like int, long & double), the "thing" is stored in one
part, so that it takes of just the space it needs (int & long is 4 bytes,
double is 8 bytes for more 32-bit computers). The space is allocated as
soon as the variable is declared.

For reference types (all class-based objects), the "thing" is stored in
two parts, the reference & the object itself. The object is only allocated
when you call new, but the reference works just like a value type: Space for
it is allocated as soon as it's declared, and that will be 32-bits on a
32-bit system;

SO:

int a; // 4 bytes
int b = 1; // 4 bytes;
double c; // 8 bytes
double d = 1.0; // 8 bytes
object e; // 4 bytes
object f = null; // 4 bytes
object g = new Double(1.0); // 4 bytes + 8 bytes= 12 bytes total;

Hence this method isn't a practical way of saving memory (but see my other
post on the topic).

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Jazzper is onto something, although his solution is way off the mark.

There are a couple way to handle this. None is particularly pretty.

Method A:
Define every conceivable collection of fields you may need, and create
separated class for each, all derived from a common base class. Create the
smallest class that have what you need, and store it in a reference to the
base class. You will later of to downcast that reference to a object of the
specific type you need.

Method B:
Store all the fields of one type in an array of undefined size,
defining enums for each element at access them.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
I think there would be too many combinations to define a different
child class for each possible combination.

Following on from your second suggestion, I could maybe create a class
like:

public class Record
{
public int[] intVals;
public long[] longVals;
public float[] floatVals;
public double[] doubleVals;
}

And outside of the class store the location of the data I want to
access, i.e.

myValue => float array, pos 4
myArray => double array, start pos 5, len 3

<<Questions>>
If my record is only to store a single int value (i.e. pos 0 of the
int array), how many bytes will the class take up?

Should I use a struct instead of a class?


thanks,
Steve.
 
Steve said:
I think there would be too many combinations to define a different
child class for each possible combination.

You'll probably be much better off with the first method. You don't
need to define every possible combination, just the most popular sizes.
Then choose the smallest one which has every field you need.

If the collection of fields is very diverse, perhaps you should abandon
a struct entirely and store the data as XML fragments.


--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Back
Top