473,416 Members | 1,531 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,416 software developers and data experts.

Calculating memory utilization

Say I have a class like,

class Sample
{
public decimal first = 10;
public decimal second = 20;
}

I have initialized it

Sample sample = new Sample();

Now how do I calculate the space required for this instance ? I have
calculated in the following way,

decimal takes 12 bytes. Here we have two decimals, hence 24 bytes. And the
variable sample, I believe it takes 4bytes. So total it is 28bytes. Is this
the right way to calculate size utilized ?

Any help would be great

Thanks

Aug 14 '08 #1
4 2071
On Thu, 14 Aug 2008 08:59:00 -0700, Navaneeth.K.N
<Na*********@discussions.microsoft.comwrote:
[...]
Now how do I calculate the space required for this instance ? I have
calculated in the following way,

decimal takes 12 bytes. Here we have two decimals, hence 24 bytes. And
the
variable sample, I believe it takes 4bytes. So total it is 28bytes. Is
this
the right way to calculate size utilized ?
It depends on why you want the size. But as a general rule, I'd say no,
this probably isn't the size you want. The variables that may reference a
class instance come and go, and so trying to include them in the total
size for the class doesn't make much sense.

If you can explain why you think knowing the size of the class is useful,
it might be possible to provide an answer that more precisely meets your
needs.

Pete
Aug 14 '08 #2
"Peter Duniho" wrote:
If you can explain why you think knowing the size of the class is useful,
it might be possible to provide an answer that more precisely meets your
needs.

Pete
Thanks peter.

There are two reasons why I am asking it. First one is purely for academic
interest. Second, I am working on a pocket PC application which has very less
memory. So my boss is asking me to find where the bottleneck is.

I know there would be many instances and calculating the class size as a
whole doesn't make any sense. But assume we have only one instance, then how
do we go about calculating the size ?

Thanks
Aug 14 '08 #3
I have no idea if this will help you or not since I have not used it
but I heard good things about it. It’s a memory profiler and can be
found here:

http://memprofiler.com/

It has a free trial so you can install it and see if it tells you what
you need to know.

On Aug 14, 10:59*am, Navaneeth.K.N
<Navaneet...@discussions.microsoft.comwrote:
Say I have a class like,

class Sample
{
* * public decimal first = 10;
* * public decimal second = 20;

}

I have initialized it

Sample sample = new Sample();

Now how do I calculate the space required for this instance ? I have
calculated in the following way,

decimal takes 12 bytes. Here we have two decimals, hence 24 bytes. And the
variable sample, I believe it takes 4bytes. So total it is 28bytes. Is this
the right way to calculate size utilized ?

Any help would be great

Thanks
Aug 14 '08 #4
Navaneeth.K.N wrote:
"Peter Duniho" wrote:
>If you can explain why you think knowing the size of the class is useful,
it might be possible to provide an answer that more precisely meets your
needs.
There are two reasons why I am asking it. First one is purely for academic
interest. Second, I am working on a pocket PC application which has very less
memory. So my boss is asking me to find where the bottleneck is.

I know there would be many instances and calculating the class size as a
whole doesn't make any sense. But assume we have only one instance, then how
do we go about calculating the size ?
I think the simplest approach is to allocate a large number
of them and then measure how much memory usage increase and then
do a simple division.

The program attached seems to indicate that:

mem usage = 4 byte for ref + 8 byte object overhead + size of data

on my version of .NET and several unknown factors. I am sure
there are other factors that influence memory usage.

Arne

=================================================

using System;

namespace E
{
public class Sizer<Twhere T : class,new()
{
private const int N = 100000;
public static int Calc()
{
GC.Collect();
long m1 = GC.GetTotalMemory(false);
T[] a = new T[N];
for(int i = 0; i < N; i++)
{
a[i] = new T();
}
long m2 = GC.GetTotalMemory(false);
return (int)((m2 - m1) / N);
}
}
public class A
{
private int iv;
public int Iv {
get { return iv; }
set { iv = value; }
}
}
public class B
{
private int iv;
private double xv;
public int Iv
{
get { return iv; }
set { iv = value; }
}
public double Xv
{
get { return xv; }
set { xv = value; }
}
}
public class C
{
private int iv;
public int Iv {
get { return iv; }
set { iv = value; }
}
public virtual void Foo() { }
public virtual void Bar() { }
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Sizer<A>.Calc());
Console.WriteLine(Sizer<B>.Calc());
Console.WriteLine(Sizer<C>.Calc());
Console.ReadKey();
}
}
}
Aug 16 '08 #5

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

Similar topics

3
by: Thomas Moy | last post by:
What do people think is normal for memory utilization? I know that's too broad, so here are some basics. MS SQL Server 2000, Windows 2000 Server, 2GB RAM Db 1, size = 2.0 GB Db 2, size =...
11
by: jong | last post by:
I have a problem with an C# application. The application is a 24x7 low volume message processing server. The server has a single thread of processing, running in a continuous loop, for each...
2
by: Arthur M. | last post by:
Does anyone know of a way to pin point true memory utilization on a per object / allocation basis in dot net. The problem that i'm having is projecting the amount of memory that will be required...
1
by: 1944USA | last post by:
I have a C# application written as a Windows Service. It spawns a number of threads, each thread has very large memory intensive processes running on it. I can keep adding threads as long as the...
2
by: rizjabbar | last post by:
I have a memory leak happening... I believe it is due to Dom parser... could anyone help me with this: Do I need a delete??? /////////////////////////////////////////////// //Code on Main HTML...
1
by: nazgul | last post by:
Hi all, I have an app that runs on multiple boxes. On my slackware box, running Python 2.5.1, top shows this: Mem: 1002736k total, 453268k used, 549468k free, 31392k buffers Swap: ...
0
by: Sam Samson | last post by:
Hi All, I wrote a little monitoring app in c# .. one of the things its supposed to do is report back how much memory the PC its on is using. my first pass at this was to iterate through the...
2
by: =?Utf-8?B?Um9oaXQ=?= | last post by:
..NET is a memory hog - plain and simple. This, in my opinion, makes it less than ideal for embedded applications where memory is constrained. I am forced to use .NET for reasons that I will not...
1
by: Kaheru | last post by:
memory utilization increase? This is because when i try to keep track of the CPU utilization and memory utilization of my FTP server process (ftpserver.exe), the CPU utilization increase, but the...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.