By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

Calculating size in bytes of the contents a datastructure

P: n/a
Hi,

I have created my own data structure. Basically it is a two way
'stack', where I can push elements and pop elements both from top and
bottom. I also included a counter to show the number of elements
currently on the data structure.

I need to calculate the size in bytes of this datastructure content at
a certain time. What I am pushing on this datastructure is the same
class instances but with different values.

How can I achieve this.
Thanks in Advance

Mar 1 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

That's a tricky question, the first doubt I get is why you need to know
this?

First you need to know especify if you want the sum of the structure and all
the instances or just the structure.
The size of the structure depend of the way you store the collection, what
you use? an array, an ArrayList or something else?

the same thing apply to the instances you are storing, you may want to know
the memory footprint of that instance with/without the inclusion of the
referenced inside it, think, what would happen if the store instance has a
string member, the size of this strnig will change from instance to
instance.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,

I have created my own data structure. Basically it is a two way
'stack', where I can push elements and pop elements both from top and
bottom. I also included a counter to show the number of elements
currently on the data structure.

I need to calculate the size in bytes of this datastructure content at
a certain time. What I am pushing on this datastructure is the same
class instances but with different values.

How can I achieve this.
Thanks in Advance

Mar 1 '06 #2

P: n/a
What is required is the sum of the structure and all the instances
inside it.

The code I am using is the following:

I will have an instance of the Stack class given below with data inside
it.
internal class Node
{
# region Private Fields

private object data;
private Node previous;
private Node next;

# endregion
# region Constructors

/// <summary>
/// Creates an instance of the Node class.
/// </summary>
/// <param name="obj">The value to be placed on stack</param>
public Node(object obj)
{
Data = obj;
Previous = null;
Next = null;
} // end constructor

# endregion
# region Data

/// <summary>
/// The value to be placed on stack.
/// </summary>
public object Data
{
set
{
data = value;
} // end set
get
{
return data;
} // end get
} // end property Data

/// <summary>
/// The node previous to this node.
/// </summary>
public Node Previous
{
set
{
previous = value;
} // end set
get
{
return previous;
} // end get
} // end property Previous

/// <summary>
/// The Node next to this node.
/// </summary>
public Node Next
{
set
{
next = value;
} // end set
get
{
return next;
} // end get
} // end property Next

# endregion

} // end class Node
// ***************************************
public class Stack
{
# region Private Fields

private int count;
private Node bottomStack;
private Node topStack;

# endregion

# region Constructors

/// <summary>
/// Creates an instance of the Stack class.
/// </summary>
public Stack()
{
count = 0;
bottomStack = null;
topStack = null;
} // end constructor

# endregion
# region Public Methods

/// <summary>
/// Inserts an object at the top of the stack.
/// </summary>
/// <param name="obj"></param>
public void Push(object obj)
{
Node n = new Node(obj);

if (bottomStack == null)
{
bottomStack = n;
topStack = n;
} // end if
else
{
topStack.Next = n;
n.Previous = topStack;
topStack = topStack.Next;
} // end else

Count++;
} // end method Push

/// <summary>
/// Returns the object that is found on top of the stack.
/// </summary>
/// <returns></returns>
public object PeekTop()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperationException("Stack empty.");
} // end if
else
{
obj = topStack.Data;
} // end else

return obj;
} // end method PeekTop

/// <summary>
/// Removes and returns the object that is found on top of the stack.
/// </summary>
/// <returns></returns>
public object PopTop()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperationException("Stack empty.");
} // end if
else
{
obj = topStack.Data;
if (Count == 1)
{
topStack = null;
bottomStack = null;
} // end if
else
{
topStack = topStack.Previous;
topStack.Next = null;
} // end else if

Count--;
} // end else

return obj;
} // end method PopTop

/// <summary>
/// Returns the object that is found on the bottom of the stack.
/// </summary>
/// <returns></returns>
public object PeekBottom()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperationException("Stack empty.");
} // end if
else
{
obj = bottomStack.Data;
} // end else

return obj;
} // end method PeekBottom

/// <summary>
/// Removes and returns the object that is found on the bottom of the
stack.
/// </summary>
/// <returns></returns>
public object PopBottom()
{
object obj;

obj = null;
if (Count == 0)
{
throw new InvalidOperationException("Stack empty.");
} // end if
else
{
obj = bottomStack.Data;
if (Count == 1)
{
topStack = null;
bottomStack = null;
} // end if
else
{
bottomStack = bottomStack.Next;
bottomStack.Previous = null;
} // end else

Count--;
} // end else

return obj;
} // end method PopBottom

/// <summary>
/// Removes all objects from the stack.
/// </summary>
public void Clear()
{
bottomStack = null;
topStack = null;
Count = 0;
} // end method Clear

# endregion
# region Properties

/// <summary>
/// Holds the current size of the Stack
/// </summary>
public int Count
{
set
{
count = value;
} // end set
get
{
return count;
} // end get

} // end property Count

# endregion

} // end class Stack

Mar 1 '06 #3

P: n/a
You could also just use LinkedList<T> to create a double-ended queue and
push or pop elements off either end. As for size, you could use
BinarySerializer on each element, but that would be pretty slow. Why you
need the binary size of each element?

--
William Stacey [MVP]

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
| Hi,
|
| I have created my own data structure. Basically it is a two way
| 'stack', where I can push elements and pop elements both from top and
| bottom. I also included a counter to show the number of elements
| currently on the data structure.
|
| I need to calculate the size in bytes of this datastructure content at
| a certain time. What I am pushing on this datastructure is the same
| class instances but with different values.
|
| How can I achieve this.
| Thanks in Advance
|
Mar 1 '06 #4

P: n/a
Regarding the LinkedList datastructure, in which namespace is this
available is this available?
(Currently using VS2003, .Net 1.1)

Basically I have two algorithms:
1) one that simply pushes elements on the data strcutre and pops
nothing
2) the other pushes elements on the data structure and pops old
elements

Now I need to monitor this performance issue against execution time to
show which is most appropriate in certain sitations. If there is
anything else more suitable than binary size, then its not going to
make me a problem.

Any further suggestions
Thanks

Mar 1 '06 #5

P: n/a
| Regarding the LinkedList datastructure, in which namespace is this
| available is this available?
| (Currently using VS2003, .Net 1.1)

..Net 2.0. A version may also be in PowerCollections. Not sure.
| Now I need to monitor this performance issue against execution time to
| show which is most appropriate in certain sitations. If there is
| anything else more suitable than binary size, then its not going to
| make me a problem.

I am still not understanding. Why you need the size the objects?
Mar 1 '06 #6

P: n/a
HI,
"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
Regarding the LinkedList datastructure, in which namespace is this
available is this available?
I don;t find it in 1.1 , maybe 2.0 ?
(Currently using VS2003, .Net 1.1)

Basically I have two algorithms:
1) one that simply pushes elements on the data strcutre and pops
nothing
2) the other pushes elements on the data structure and pops old
elements So you have only one element? what about the first one?

If you talk about push/pop I think in either a Stack of a Queue (in the
latter is called Enqeue/Dequeue )
Now I need to monitor this performance issue what performance issue? this is the first time you mention it
against execution time to
show which is most appropriate in certain sitations. If there is
anything else more suitable than binary size, then its not going to
make me a problem.


What are you trying to do in the first place
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 1 '06 #7

P: n/a
As mentioned I will have 2 algorithms. Now I will have different input
data varying in size.

This data is to be applied to both algorithms. The algorithms have
similar behaviour, where they will use the input data to do some
execution, upon that input just executed some information is generated
and needs to be stored on the datastructure. The main reason to store
this information is that while the algorithms are executing, there may
rise situations to rollback and so pop information from the head of the
data structure till algorithm comes back to a state that is correct.

Now, there will be a time that some information stored on
datastructure(found on the bottom) has become old and there will surely
be no need to use again that data to rollback.

Now one of the algorithms is to keep all the information required to
rollback, while the other when some rollback data is not required
anymore is to be removed.

I need to monitor the two algorithms to compare and contrast
performance issues.

I hope this is more clear.

Mar 2 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.