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

Big memory overhead

Hello,

I'm writing an app running both under .NET and Compact Frameword. I need to allocate lot of little mem blocks. They form a n-ary tree, i.e. pointing at each other. The mem alloc by the program is just prepostrous - it's about 4times more than I esimate these objects should take. On PC it's not such a big deal but on the PPC 32MB device it's quite a problem. Does anyone know it there's something to do about it? Or why it is happening?

tnhx
Nov 16 '05 #1
12 1728
Mikolas <Mi*****@discussions.microsoft.com> wrote:
I'm writing an app running both under .NET and Compact Frameword. I
need to allocate lot of little mem blocks. They form a n-ary tree,
i.e. pointing at each other. The mem alloc by the program is just
prepostrous - it's about 4times more than I esimate these objects
should take. On PC it's not such a big deal but on the PPC 32MB
device it's quite a problem. Does anyone know it there's something to
do about it? Or why it is happening?


Could you give an example of the kind of object you're allocating, and
how you're estimating the required memory usage?

--
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
Mikolas <Mi*****@discussions.microsoft.com> wrote:
For example one of the classes these data members :
{
uint sectionID; // my estimate 4B
string sectionName; // dunno, but hope not much more than 2*length

ArrayList subsections;//my estimate 10B + capacity*4
ArrayList machines;//my estimate 10B + capacity*4
ArrayList graphicObjs;//my estimate 10B + capacity*4
IDMap graphicObjsToSubsections;//my estimate 10B + capacity*4
IDMap graphicObjsToMachines;//my estimate 10B + capacity*4


Okay. For one thing, there's 8 bytes overhead for the class itself.
Then for each reference, there's a 4 byte overhead. string takes
12 bytes + (length+1)*2 rounded up to a multiple of 4
Each ArrayList is going to be 24 + capacity*4 (I think). IDMap will
depend on the implementation, obviously.

So, how "out" your estimates are really depend on the size of strings
and the capacity of your ArrayLists. Could you give a sample object and
what size you're seeing, and we'll see if we can explain it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Keep in mind that strings in C# are wide character strings, so there's you
minimum 2*length. The you need to add in data members like Length (which
I'm sure is not recaclulated everytime the Length property is accessed) and
the internal member to stroe the address of the string, plus a minimum of 2
bytes per character. Even in C++ string objects can be heavy.

The ArrayLists I don't know about. I guess it depends on the memory
management of the object and how it was coded(performance vs. size) Again
keep in mind internal members such as size, reference, and synconizer
objects. Also what are the lists storing? Capacity * 4 makes me assume
refernce types but if they are value types or structs it can be larger.

..02
jim

"Mikolas" <Mi*****@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
For example one of the classes these data members :
{
uint sectionID; // my estimate 4B
string sectionName; // dunno, but hope not much more than 2*length

ArrayList subsections;//my estimate 10B + capacity*4
ArrayList machines;//my estimate 10B + capacity*4
ArrayList graphicObjs;//my estimate 10B + capacity*4
IDMap graphicObjsToSubsections;//my estimate 10B + capacity*4
IDMap graphicObjsToMachines;//my estimate 10B + capacity*4

mikolas

"Jon Skeet [C# MVP]" wrote:
Mikolas <Mi*****@discussions.microsoft.com> wrote:
I'm writing an app running both under .NET and Compact Frameword. I
need to allocate lot of little mem blocks. They form a n-ary tree,
i.e. pointing at each other. The mem alloc by the program is just
prepostrous - it's about 4times more than I esimate these objects
should take. On PC it's not such a big deal but on the PPC 32MB
device it's quite a problem. Does anyone know it there's something to
do about it? Or why it is happening?


Could you give an example of the kind of object you're allocating, and
how you're estimating the required memory usage?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Jim H <no****@jimsaccount.com> wrote:
Keep in mind that strings in C# are wide character strings, so there's you
minimum 2*length. The you need to add in data members like Length (which
I'm sure is not recaclulated everytime the Length property is accessed) and
the internal member to stroe the address of the string, plus a minimum of 2
bytes per character. Even in C++ string objects can be heavy.
There's no internal member to store the address of the string - strings
are special in that the size of the string object itself varies
according to the size. The data for the string is within the string
object itself (unlike Java, where String contains a char array
reference).
The ArrayLists I don't know about. I guess it depends on the memory
management of the object and how it was coded(performance vs. size) Again
keep in mind internal members such as size, reference, and synconizer
objects. Also what are the lists storing? Capacity * 4 makes me assume
refernce types but if they are value types or structs it can be larger.


No, the size of the ArrayList's data array will always be capacity*4
because it *can* only store references - value types end up getting
boxed (assuming we're not talking about v2 with generics).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Mikolas <Mi*****@discussions.microsoft.com> wrote:
So, how "out" your estimates are really depend on the size of strings
and the capacity of your ArrayLists. Could you give a sample object and
what size you're seeing, and we'll see if we can explain it?
I created a sample binary tree with 2^17 nodes like mentioned above,
when created the the mem usage in Task list rises by 56MB. This could
also be because there's still plenty of free mem. On PPC though, when
I create 2^16 nodes the mem usage rises by 13MB and there's almost
nothing left(from 32MB).


But what is the contents of the ArrayLists and strings in that case?
That's only 488 bytes per node, which doesn't sound very much to me -
you don't need to have very large ArrayLists to see that.
You can easily get similar results from
<code>
class MyTree
{
string name;
MyTree left, right;
MyTree(string name)
{
this.name = name;
}

public static MyTree CreateSample(string name, int depth)
{
if (depth <= 0)
return null;

MyTree mt = new MyTree(name);

mt.left = CreateSample(name + "0", depth - 1);
mt.right = CreateSample(name + "1", depth - 1);

return mt;
}
}
MyTree mt;
private void button1_Click(object sender, System.EventArgs e)
{
this.mt = MyTree.CreateSample("root", 20);

}
</code>


And what results did you get from that? (I haven't got time to run it
myself right now.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Thanks. I guess I shouldn't post assumptions.
Sorry,
jim

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jim H <no****@jimsaccount.com> wrote:
Keep in mind that strings in C# are wide character strings, so there's you minimum 2*length. The you need to add in data members like Length (which I'm sure is not recaclulated everytime the Length property is accessed) and the internal member to stroe the address of the string, plus a minimum of 2 bytes per character. Even in C++ string objects can be heavy.


There's no internal member to store the address of the string - strings
are special in that the size of the string object itself varies
according to the size. The data for the string is within the string
object itself (unlike Java, where String contains a char array
reference).
The ArrayLists I don't know about. I guess it depends on the memory
management of the object and how it was coded(performance vs. size) Again keep in mind internal members such as size, reference, and synconizer
objects. Also what are the lists storing? Capacity * 4 makes me assume
refernce types but if they are value types or structs it can be larger.


No, the size of the ArrayList's data array will always be capacity*4
because it *can* only store references - value types end up getting
boxed (assuming we're not talking about v2 with generics).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jim H <no****@jimsaccount.com> wrote: No, the size of the ArrayList's data array will always be capacity*4
because it *can* only store references - value types end up getting
boxed (assuming we're not talking about v2 with generics).


Jon,

The exact size of the ArrayList's data array (Object[]) is actualy; 16 +
capacity*4.

Or:
0 Int32 Sync#
4 Int32 MT ptr. (method table ptr. Object[])
8 Int32 Component size
12 Int32 MT ptr. (Method table pointer of element types
(Object))
16 Int32 elem1
20 Int32 elem2
.....

Willy.
Nov 16 '05 #8

Did you start with an initial size for the ArrayList?
If you don't, your first AL (actually the items Object[]) will have an
initial size of 16 + 4*4bytes, when this array get full the CLR will expand
this array using a simple algorithm ((actual number of elements * component
size) * 2), so yes, it grows exponential.
See where this goes to?

Willy.

"Mikolas" <Mi*****@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...


"Jon Skeet [C# MVP]" wrote:
Mikolas <Mi*****@discussions.microsoft.com> wrote:
> For example one of the classes these data members :
> {
> uint sectionID; // my estimate 4B
> string sectionName; // dunno, but hope not much more than 2*length
>
> ArrayList subsections;//my estimate 10B + capacity*4
> ArrayList machines;//my estimate 10B + capacity*4
> ArrayList graphicObjs;//my estimate 10B + capacity*4
> IDMap graphicObjsToSubsections;//my estimate 10B + capacity*4
> IDMap graphicObjsToMachines;//my estimate 10B + capacity*4


Okay. For one thing, there's 8 bytes overhead for the class itself.
Then for each reference, there's a 4 byte overhead. string takes
12 bytes + (length+1)*2 rounded up to a multiple of 4
Each ArrayList is going to be 24 + capacity*4 (I think). IDMap will
depend on the implementation, obviously.

So, how "out" your estimates are really depend on the size of strings
and the capacity of your ArrayLists. Could you give a sample object and
what size you're seeing, and we'll see if we can explain it?


I created a sample binary tree with 2^17 nodes like mentioned above, when
created the the mem usage in Task list rises by 56MB. This could also be
because there's still plenty of free mem.
On PPC though, when I create 2^16 nodes the mem usage rises by 13MB and
there's almost nothing left(from 32MB).

You can easily get similar results from
<code>
class MyTree
{
string name;
MyTree left, right;
MyTree(string name)
{
this.name = name;
}

public static MyTree CreateSample(string name, int depth)
{
if (depth <= 0)
return null;

MyTree mt = new MyTree(name);

mt.left = CreateSample(name + "0", depth - 1);
mt.right = CreateSample(name + "1", depth - 1);

return mt;
}
}
MyTree mt;
private void button1_Click(object sender, System.EventArgs e)
{
this.mt = MyTree.CreateSample("root", 20);

}
</code>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
No, the size of the ArrayList's data array will always be capacity*4
because it *can* only store references - value types end up getting
boxed (assuming we're not talking about v2 with generics).


The exact size of the ArrayList's data array (Object[]) is actualy; 16 +
capacity*4.

Or:
0 Int32 Sync#
4 Int32 MT ptr. (method table ptr. Object[])
8 Int32 Component size
12 Int32 MT ptr. (Method table pointer of element types
(Object))
16 Int32 elem1
20 Int32 elem2
....


Apologies, yes, I forgot about the normal overhead, length and type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10


"Willy Denoyette [MVP]" wrote:

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jim H <no****@jimsaccount.com> wrote:
No, the size of the ArrayList's data array will always be capacity*4
because it *can* only store references - value types end up getting
boxed (assuming we're not talking about v2 with generics).


Jon,

The exact size of the ArrayList's data array (Object[]) is actualy; 16 +
capacity*4.

Or:
0 Int32 Sync#
4 Int32 MT ptr. (method table ptr. Object[])
8 Int32 Component size
12 Int32 MT ptr. (Method table pointer of element types
(Object))
16 Int32 elem1
20 Int32 elem2
.....

Willy.

Is there any tool how to do "sizeof" then?

thnx

Nov 16 '05 #11
Mikolas <Mi*****@discussions.microsoft.com> wrote:
Is there any tool how to do "sizeof" then?


No, not really. I find the best way to judge is to create a lot of
objects and look at the memory difference. Change the number of objects
to make sure it's linear, otherwise you could get phased by overheads
etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
A tool? Yes, the native debugger check
http://www.microsoft.com/whdc/devtoo...g/default.mspx
and this article http://msdn.microsoft.com/msdnmag/is.../06/Bugslayer/
to get you started.
Willy.

"Mikolas" <Mi*****@discussions.microsoft.com> wrote in message
news:90**********************************@microsof t.com...


"Willy Denoyette [MVP]" wrote:

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
> Jim H <no****@jimsaccount.com> wrote:

> No, the size of the ArrayList's data array will always be capacity*4
> because it *can* only store references - value types end up getting
> boxed (assuming we're not talking about v2 with generics).
>


Jon,

The exact size of the ArrayList's data array (Object[]) is actualy; 16 +
capacity*4.

Or:
0 Int32 Sync#
4 Int32 MT ptr. (method table ptr. Object[])
8 Int32 Component size
12 Int32 MT ptr. (Method table pointer of element types
(Object))
16 Int32 elem1
20 Int32 elem2
.....

Willy.

Is there any tool how to do "sizeof" then?

thnx


Nov 16 '05 #13

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

Similar topics

3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
9
by: Evangelista Sami | last post by:
hello everybody what is the size of the memory allocated for this declaration int array; it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer) or sizeof(int)...
4
by: Evangelista Sami | last post by:
hello all i am implementing an application in which a large number of (char *) is stored into a hash table structure. As there may be collisions, each (char *) inserted into my hash table is...
17
by: Frank Rizzo | last post by:
Hello, doing some profiling work and trying to resolve the memory usage balooning through the roof. Just want to make sure that my reasoning is correct. My question is as follows: Let's say I...
6
by: Mohitz | last post by:
Hi, Need views on the following.. I need to design a data structure to store the values of n attributes, where n is something near 20. Now, all the attributes are optional. So, at times, i...
5
by: Sune | last post by:
Hi all, I want to make data stored in-memory (not disk) available to several processes. My concern is that poorly written C applications with dangling pointers may(will) damage the data in this...
19
by: jsanshef | last post by:
Hi, after a couple of days of script debugging, I kind of found that some assumptions I was doing about the memory complexity of my classes are not true. I decided to do a simple script to...
12
by: mohitanchlia | last post by:
I have written a program that loads a file having 3 columns into a map. Here is the declaration: struct strE { char iEID; char acEN; int iDed; };
5
by: Santiago Romero | last post by:
Is there a way to check the REAL size in memory of a python object? Something like or or something like that ...
8
by: Horacius ReX | last post by:
Hi, I am developing some code in C which first I compile on my linux machine and afterwards I test on another special hardware which has almost no debug capabilities at all. Usually I get a lot...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.