473,396 Members | 1,861 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.

Class Memory Usage

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,
Nov 16 '05 #1
5 4890
Jazzper <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jazzper" <an*******@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.com...
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!)
Nov 16 '05 #3
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!)
"Steve" <sh*****@tssg.org> wrote in message
news:ca**************************@posting.google.c om...
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,

Nov 16 '05 #4
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.
Nov 16 '05 #5
"Steve" <sh*****@tssg.org> wrote in message
news:ca*************************@posting.google.co m...
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!)
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tomvr | last post by:
Hello I have noticed some 'weird' memory usage in a vb.net windows app The situation is as follows I have an app (heavy on images) with 2 forms (actually there are more forms and on starting...
6
by: Tom | last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB Memory Footprint. It starts a window runs a process and does a regular expression. I have done a GC.Collect to make sure that,...
1
by: Marcos Boyington | last post by:
Hey guys, quick question about how C++ designers chose to not implement this, I'm sure there are some people here who can take a better guess at it than I. So, in C++, the only way to prevent...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
2
by: Colin McKinnon | last post by:
Hi all, I am currently working on a project which involves using a lot of code on each page. I'm currently refactoring with a view to performance and have discovered that when my app starts to...
4
by: Devon Null | last post by:
I have been exploring the concept of abstract classes and I was curious - If I do not define a base class as abstract, will it be instantiated (hope that is the right word) when a derived class is...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
2
by: manojmohadikar2008 | last post by:
Hi All, We are observing a serious issue with the memory usage of Queue and its very critical issue which needs to be fixed. We have an application which runs two threads i.e. a Producer and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.