如何在vb.net中获取类对象的内存大小 [英] how to get class object memory size in vb.net

查看:298
本文介绍了如何在vb.net中获取类对象的内存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公开  testTemplate 

公开 tid 作为
公开 emp01( 400 作为 字节
公开 emp02( 400 作为 字节

结束





现在我宣布testTemplate类的一个对象



  Dim  t 正如  testTemplate 





现在我只是想知道我该如何计算在vb.net中使用的内存或对象大小

使用vb.net函数。

解决方案

基本上你不能,没有一些公平高级使用Reflection来递归地解析你的类型,并获得每个组成部分的大小 - 这可能有他们自己包含的类,这可能......你明白了。



这将是懒惰的。



显然还有另一种方式。看看这个: http://blog.softwx.net/ 2013/01 / benchmarking-struct-and-object-sizes.html [ ^ ]它是C#,而不是VB,但它确实可以完成这项工作 - 显然,我还没有尝试过。我对它的准确性有严重的怀疑,因为代码最多会尝试12次才能在同一个对象上获得一致的结果......如果GC不会连续两次告诉你同样的事情,你有什么希望得到的?



.NET的一部分想法是,对于大多数应用程序,你不应该考虑对象的大小 - 只是开始使用它们

Public Class testTemplate

    Public tid As Long
    Public emp01(400) As Byte
    Public emp02(400) As Byte

End Class



now i declare a object of the testTemplate class

Dim t As New testTemplate



now i just want to know how can i calculate the memory used or object size in vb.net
using vb.net function.

解决方案

Basically you can't, without some fairly advanced use of Reflection to recursively parse your type and get the sizes of each of it's constituent parts - which may well have their own contained classes, which may... you get the idea.

And it'll be sloooow.

There is apparently another way. Have a look at this: http://blog.softwx.net/2013/01/benchmarking-c-struct-and-object-sizes.html[^] it's C#, not VB, but it does do the job - apparently, I haven't tried it. I have serious doubts about it's accuracy, given that the code tries up to twelve times to get a consistent result on the same object...if the GC won't tell you the same thing twice in a row, what hope have you got?

Part of the idea of .NET is that for most applications, you aren't supposed to think about the size of your objects - just get on an use them.


这篇关于如何在vb.net中获取类对象的内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆