你怎么知道一个班级使用了多少内存? [英] How do you figure out how much memory a class is using?

查看:65
本文介绍了你怎么知道一个班级使用了多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何计算一个类的给定实例

使用多少内存。


例如,如果我加载一个包含10个项目的集合类,它可能会使用1KB,
如果我加载1000个项目,它可能会使用100KB。

如何测量使用的内存量一旦上课了吗?


谢谢!

-

Joe Fallon


解决方案

*" Joe Fallon" < JF ****** @ nospamtwcny.rr.com> scripsit:

我想知道如何计算一个类的给定实例使用的内存量。

例如,如果我加载一个包含10个项目的集合类,它可能会使用1KB,
如果我加载1000个项目,它可能会使用100KB。
如何测量加载类后使用的内存量?



您所能做的就是使用内存分析器。我不确定这是否会像你期望的那样工作。


< URL:http://www.gotdotnet.com/Community /UserSamples/Details.aspx?SampleGuid=3254325d-a4aa-4bb3-aa86-c72d5104ec74>


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>


Herfried,

谢谢回复。


我正在尝试这样的代码:

(感谢任何评论。)


Dim byteCount As Long = System.GC.GetTotalMemory(False)

mCollection = Collection.GetCollection()

byteCount = System.GC.GetTotalMemory(False) - byteCount


Dim Collectionkb Double = byteCount / 1024

我连续几次运行这段代码并获得不同的答案。

65KB,59KB,43KB,47KB,47KB,48KB

当我将类序列化为磁盘上的文件时,它是30KB。

我认为经验法则是文件大小x 2 =内存中的大小。


您能想出一种更可靠的方法来确定运行时

a类使用的RAM量吗?

我正在尝试决定是否应该将该集合保存在RAM中或将其序列化为

到磁盘。

我想在运行时根据它来决定超过一定金额的内存。

-

Joe Fallon


" Herfried K. Wagner [MVP]" <喜*************** @ gmx.at>在消息中写道

news:c6 ************ @ ID-208219.news.uni-berlin.de ...

* 乔法伦 < JF ****** @ nospamtwcny.rr.com> scripsit:

我想知道如何计算一个类的给定
实例使用的内存量。

例如,如果我加载一个包含10个项目的集合类,它可能会使用
1KB,如果我加载1000个项目,它可能会使用100KB。
如何测量加载类后使用的内存量?您所能做的就是使用内存分析器。我不确定这是否会像你期望的那样工作。



< URL: http://www.gotdotnet.com/Community/U...px?SampleGuid=

3254325d-a4aa-4bb3-aa86-c72d5104ec74>
-
Herfried K. Wagner [MVP]
< URL:http://dotnet.mvps .org />



Joe,

当我将类序列化为磁盘上的文件时,它是30KB。我认为经验法则是文件大小x 2 =内存大小。
看看实际序列化的是什么,二进制的额外开销是b / b
序列化,因为它存储了Type&流中的属性名称。

我正在尝试决定是否应该将该集合保存在RAM中或将
序列化为磁盘。
我想在运行时决定这个基于它超过一定量的RAM。
各个元素的大小是固定的吗?我会有一个配置阈值

选项元素数量,如果集合点击元素数量

我会去磁盘,否则我会把它保存在内存中。 ..


希望这有帮助

Jay


" Joe Fallon" < JF ****** @ nospamtwcny.rr.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ... Herfried,
感谢您的回复。

我正在尝试这样的代码:
(感谢任何评论。)

Dim byteCount As Long = System.GC.GetTotalMemory(False)
mCollection = Collection.GetCollection()

byteCount = System.GC.GetTotalMemory(False) - byteCount

Dim Collectionkb As Double = byteCount / 1024
65KB,59KB,43KB,47KB,47KB,48KB
当我将类序列化为磁盘上的文件时它是30KB。
我认为经验法则是文件大小x 2 =内存中的大小。

你能想出更可靠的方法来确定使用的RAM量$ b一个类在运行时$ b?
我正在尝试决定是否应该将该集合保存在RAM中,或者将
序列化为磁盘。
我想在运行时基于此决定它超过了一定量的RAM。 -
Joe Fallon

Herfried K. Wagner [MVP]" <喜*************** @ gmx.at>在消息中写道
新闻:c6 ************ @ ID-208219.news.uni-berlin.de ...

*Joe Fallon ; < JF ****** @ nospamtwcny.rr.com> scripsit:

我想知道如何计算出一个类的给定实例使用了多少内存。

例如,如果我加载一个集合类10个项目可能会使用1KB,如果我加载1000个项目,它可能会使用100KB。
如何测量加载类后使用的内存量?
你所能做的就是使用一个内存分析器。我不确定这是否会像你期望的那样工作。



< URL: http://www.gotdotnet.com/Community/U...px?SampleGuid= 3254325d-a4aa-4bb3-aa86-c72d5104ec74>


-
Herfried K. Wagner [MVP]
< URL:http:/ /dotnet.mvps.org/>




I would like to know how you can figure out how much memory a given instance
of a class is using.

For example, if I load a collection class with 10 items it might use 1KB,
and if I load it with 1000 items it might use 100KB.
How do I measure the amount of memory used once the class is loaded?

Thanks!
--
Joe Fallon


解决方案

* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:

I would like to know how you can figure out how much memory a given instance
of a class is using.

For example, if I load a collection class with 10 items it might use 1KB,
and if I load it with 1000 items it might use 100KB.
How do I measure the amount of memory used once the class is loaded?



All you can do is using a memory profiler. I am not sure if this will
work as you expect it to do.

<URL:http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=3254325d-a4aa-4bb3-aa86-c72d5104ec74>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Herfried,
Thanks for the response.

I am trying code like this:
(Appreciate any comments.)

Dim byteCount As Long = System.GC.GetTotalMemory(False)
mCollection = Collection.GetCollection()

byteCount = System.GC.GetTotalMemory(False) - byteCount

Dim Collectionkb As Double = byteCount / 1024

I ran this code a few times in a row and got varying answers.
65KB, 59KB, 43KB, 47KB, 47KB, 48KB

When I serialize the class to a file on disk it is 30KB.
I thought the rule of thumb was that File size x 2 = Size in Memory.

Can you think of a more reliable way to determine the amount of RAM used by
a class at runtime?
I am trying to decide if I should keep the collection in RAM or serialize it
to disk.
I would like to decide this at run time based on it exceeding a certain
amount of RAM.
--
Joe Fallon

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c6************@ID-208219.news.uni-berlin.de...

* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:

I would like to know how you can figure out how much memory a given instance of a class is using.

For example, if I load a collection class with 10 items it might use 1KB, and if I load it with 1000 items it might use 100KB.
How do I measure the amount of memory used once the class is loaded?
All you can do is using a memory profiler. I am not sure if this will
work as you expect it to do.


<URL:http://www.gotdotnet.com/Community/U...px?SampleGuid=
3254325d-a4aa-4bb3-aa86-c72d5104ec74>
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>



Joe,

When I serialize the class to a file on disk it is 30KB.
I thought the rule of thumb was that File size x 2 = Size in Memory. Look at what is actually serialized, there is extra overhead in binary
serialization, as it stores Type & property names in the stream.
I am trying to decide if I should keep the collection in RAM or serialize it to disk.
I would like to decide this at run time based on it exceeding a certain
amount of RAM. Are the individual elements a fixed size? I would have a config threshold
option for number of elements, if the collection hits the number of elements
I would go to disk, otherwise I would keep it in memory...

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Herfried,
Thanks for the response.

I am trying code like this:
(Appreciate any comments.)

Dim byteCount As Long = System.GC.GetTotalMemory(False)
mCollection = Collection.GetCollection()

byteCount = System.GC.GetTotalMemory(False) - byteCount

Dim Collectionkb As Double = byteCount / 1024

I ran this code a few times in a row and got varying answers.
65KB, 59KB, 43KB, 47KB, 47KB, 48KB

When I serialize the class to a file on disk it is 30KB.
I thought the rule of thumb was that File size x 2 = Size in Memory.

Can you think of a more reliable way to determine the amount of RAM used by a class at runtime?
I am trying to decide if I should keep the collection in RAM or serialize it to disk.
I would like to decide this at run time based on it exceeding a certain
amount of RAM.
--
Joe Fallon

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c6************@ID-208219.news.uni-berlin.de...

* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:

I would like to know how you can figure out how much memory a given instance of a class is using.

For example, if I load a collection class with 10 items it might use 1KB, and if I load it with 1000 items it might use 100KB.
How do I measure the amount of memory used once the class is loaded?
All you can do is using a memory profiler. I am not sure if this will
work as you expect it to do.


<URL:http://www.gotdotnet.com/Community/U...px?SampleGuid= 3254325d-a4aa-4bb3-aa86-c72d5104ec74>


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>




这篇关于你怎么知道一个班级使用了多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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