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

Memory required to create instances of a Class

Public Class User
Private _id, _P, _L, _R As Integer
Private _balance As Int64
Private _name, _family, _tel1, _tel2, _mobile, _city, _accountNumber As
String
Private _password, _email, _ficheNumber, _postalAddress As String
Private _province, _bank As String
Private _isSpecial, _photo As Boolean
Public stk As New Stack

..
..
Properties and Functions
..
..
..
End Class

How can I calculate how much memory I need to create an instance of such
class?
Is it the sum of bits required for variables? 32+32+32+32+64+...
What about Strings and Stack?

TIA

--
Saber S.
http://maghalat.com
Nov 21 '05 #1
2 1278

"Saber" <saber[.AT.]oxin.ir> wrote in message
news:OP*************@TK2MSFTNGP09.phx.gbl...
Public Class User
Private _id, _P, _L, _R As Integer
Private _balance As Int64
Private _name, _family, _tel1, _tel2, _mobile, _city, _accountNumber As
String
Private _password, _email, _ficheNumber, _postalAddress As String
Private _province, _bank As String
Private _isSpecial, _photo As Boolean
Public stk As New Stack

.
.
Properties and Functions
.
.
.
End Class

How can I calculate how much memory I need to create an instance of such
class?
Is it the sum of bits required for variables? 32+32+32+32+64+... Value Types (Integer, Int64, Boolean) have known sizes.
What about Strings and Stack?

They are IntPtr-sized pointers to heap locations. And there is no mechanism
to determine exactly how much space they take on the heap because there's
nothing you could usefully do with this information.

Take "stk as New Stack". stk references a heap location; at that heap
location is a Stack instance. A Stack is a mix of integral fields and
pointers to objects which a are a mix of fields and pointers, etc. Moreover
multiple Use instances can share the same _name, _tel1, or stk. So even if
you could determine the size of reference types, you have to subtract out
shared instances when adding sizes together.

That being said, it's sometimes still important to have an approximation of
the memory allocation size for performance analysis. You can get an
estimate of the size by recursing the object graph and adding field sizes,
excluding reference types which are shared among instances.

Field Type - approximate size
--------------------------------
IntPtr - 4 bytes on 32bit, 8 bytes on 64bit
Integer - 4 bytes
Char - 2 bytes
String ~ IntPtr + sizeof(Integer) + sizeof(char) * length + C (but literal
strings are "interned" and shared among instances)
Stack ~ IntPtr + 2*sizeof(Integer) + sizeof(IntPtr) * length * 1.2 + C

David


Nov 21 '05 #2
Thanks David,
It was useful for me.

--
Saber S.
http://maghalat.com
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:ul**************@TK2MSFTNGP11.phx.gbl...

"Saber" <saber[.AT.]oxin.ir> wrote in message
news:OP*************@TK2MSFTNGP09.phx.gbl...
Public Class User
Private _id, _P, _L, _R As Integer
Private _balance As Int64
Private _name, _family, _tel1, _tel2, _mobile, _city, _accountNumber As
String
Private _password, _email, _ficheNumber, _postalAddress As String
Private _province, _bank As String
Private _isSpecial, _photo As Boolean
Public stk As New Stack

.
.
Properties and Functions
.
.
.
End Class

How can I calculate how much memory I need to create an instance of such
class?
Is it the sum of bits required for variables? 32+32+32+32+64+...

Value Types (Integer, Int64, Boolean) have known sizes.
What about Strings and Stack?

They are IntPtr-sized pointers to heap locations. And there is no
mechanism to determine exactly how much space they take on the heap
because there's nothing you could usefully do with this information.

Take "stk as New Stack". stk references a heap location; at that heap
location is a Stack instance. A Stack is a mix of integral fields and
pointers to objects which a are a mix of fields and pointers, etc.
Moreover multiple Use instances can share the same _name, _tel1, or stk.
So even if you could determine the size of reference types, you have to
subtract out shared instances when adding sizes together.

That being said, it's sometimes still important to have an approximation
of the memory allocation size for performance analysis. You can get an
estimate of the size by recursing the object graph and adding field sizes,
excluding reference types which are shared among instances.

Field Type - approximate size
--------------------------------
IntPtr - 4 bytes on 32bit, 8 bytes on 64bit
Integer - 4 bytes
Char - 2 bytes
String ~ IntPtr + sizeof(Integer) + sizeof(char) * length + C (but literal
strings are "interned" and shared among instances)
Stack ~ IntPtr + 2*sizeof(Integer) + sizeof(IntPtr) * length * 1.2 + C

David

Nov 21 '05 #3

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

Similar topics

25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
22
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.