查找内存使用了类的对象 [英] find memory used an object of class
问题描述
大家好
我想计算我的BST Node类中使用的内存大小:
这里是Node类代码:
class 节点
{
/// < 摘要 < span class =code-summarycomment>>
/// BST节点的类节点
/// *** Word - >哈希() - > key
/// < / summary >
public int key { get ; set ; } // 哈希词在这里
public string word { get ; set ; } // 英文单词
public string 翻译{ get ; set ; } // 翻译Peresian
public int count { get ; set ; } // 插入BST时计算节点
public 节点权限{ get ; set ; } // 指向当前节点右侧节点的指针
public 节点左{ get ; set ; } // 指向当前节点左侧节点的指针
public 节点父{ get ; set ; } // 指向当前节点的父级的指针
我的问题是指针(右,左,父),因为查找sizeof(int)和sizeof(int)非常简单。我如何计算它?
谢谢。
1.基本上你应该使用sizeof()
和/或Marshal.SizeOf()
计算它。
2.的大小
,left
和parent
属性,因为它们引用其他Node
对象应该是4(对于Win32应用程序)和8(对于Win64应用程序)。
3.您可以使用内存分析器工具来检查和优化内存分配,如下一个: http://memprofiler.com/ [ ^ ]
首先,那些不是指针,它们是参考。指针是一个非常不同的野兽,需要不安全的
代码才能使用。
和sizeof
一样 - 因为在正常情况下你不需要知道。
但是,由于这些是引用,它们有一个固定的大小 - 与字符串完全相同
。实际大小是固定的,但它取决于你的系统:对于32位系统,它是32位,对于64位系统,它是64位。
但是,这是引用的大小
而不是被引用的堆对象:
string s1 = Hello;
string s2 = 但是,因为这些是引用,它们具有固定的大小 - 与< code>字符串< / code>的完全相同。;s1和s2的大小相同 - 32或64 bits - 因为变量不是它所指的对象。
所以......对于32位系统,你的节点是28字节,64位系统它是48个字节。
但是......它可能不是,取决于框架在创建对象时如何组织事物:它可以添加填充以将整数扩展到64位,或者打包它们一起成64位字,它很可能在末尾添加填充,以32字节或64字节填充堆上的自然大小。
除非你试图使用struct
而不是类
,否则它并不真正相关 - 因为你真的没有对它占用的实际空间有很大的控制权! :笑:
hi all
I want to calculate size of memory used in my Node class of BST:
here is Node class Code:
class Node
{
/// <summary>
/// class Node for Nodes of BST
/// *** Word -> Hash() -> key
/// </summary>
public int key{ get; set; } // Hashed Word goes here
public string word{ get; set; }// word in English
public string translate{ get; set; }// Translate in Peresian
public int count{ get; set; } // Count nodes when Inserted to BST
public Node right{ get; set; } // A pointer to right node of current node
public Node left{ get; set; } // A pointer to left node of current node
public Node parent { get; set; } // A pointer to Parent of current node
my problem is pointers(right,left,parent) ,because finding sizeof(int) and sizeof(int) is very simple.How I calculate it?
thank you.
1.Basically you should usesizeof()
and/orMarshal.SizeOf()
to calculate it.
2.The size of theright
,left
andparent
properties, becuase they are reference to otherNode
objects should be 4 (for Win32 application) and 8 (for Win64 application).
3.You could use memory profiler tools to inspect and optimize the memory allocation, like the next one: http://memprofiler.com/[^]
First off, those aren't pointers, they are references. Pointers are a very different beastie and requireunsafe
code to use.
As doessizeof
- because you don't need to know under normal circumstances.
However, since these are references, they have a fixed size - exactly the same as that for astring
. The actual size is fixed, but what it is depends on your system: for 32 bit systems, it is 32 bits, for 64 bits systems it is 64.
However, this is the size of thereference
not the heap object that is being referred to:
string s1 = "Hello"; string s2 = "However, since these are references, they have a fixed size - exactly the same as that for a <code>string</code>.";s1 and s2 are the same size - 32 or 64 bits - because the variable is not the object it is referring to.
So...for a 32 bits system, your Node is 28 bytes, for a 64 bit system it's 48 bytes.
But...it may not be, depending on how the framework organised things when it creates the object: it may add padding to extend the integers to 64 bits, or pack them together into a 64 bit word, and it will quite probably add padding at the end to "fill" a natural size on the heap at 32 bytes or 64 bytes.
It's not really relevant, unless you are trying to use astruct
instead of aclass
- because you really don't have much control over the actual space it takes up! :laugh:
这篇关于查找内存使用了类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!