大内存开销 [英] Big memory overhead

查看:62
本文介绍了大内存开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在编写一个在.NET和Compact Frameword下运行的应用程序。我需要分配很多小内存块。它们形成一棵n-ary树,即彼此指向。该程序的内存分配只是一个荒谬的 - 它比我估计这些对象应该采取的大约4倍。在PC上它并不是一个大问题,但在PPC 32MB设备上它是一个非常大的问题。有谁知道它有关于它的事情?或者为什么会这样?


tnhx

解决方案

Mikolas< Mi ***** @ discussions.microsoft.com>写道:

我正在编写一个在.NET和Compact Frameword下运行的应用程序。我需要分配很多小内存块。它们形成一棵n树,即彼此指向的树。该程序的内存分配只是一个充满意义的东西 - 它比我估计应该采取的这些对象大约多4倍。在PC上它并不是什么大问题,但在PPC 32MB设备上这是一个非常大的问题。有谁知道它有什么可以做的吗?或者为什么会发生这种情况?




你能给出一个你要分配的对象的例子吗?

你怎么样' '估计所需的内存使用量?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该组,请不要给我发邮件


Mikolas< Mi ***** @ discussion.microsoft.com>写道:

例如,这些数据成员中的一个类:
{
uint sectionID; //我的估计4B
string sectionName; // dunno,但希望不超过2 *长度

ArrayList小节; //我的估计10B +容量* 4
ArrayList机器; //我估计10B +容量* 4
ArrayList graphicObjs; //我的估计10B +容量* 4
IDMap graphicObjsToSubsections; //我估计10B +容量* 4
IDMap graphicObjsToMachines; //我估计10B +容量* 4



好​​的。首先,类本身有8个字节的开销。

然后对于每个引用,都有4字节的开销。字符串需要

12字节+(长度+ 1)* 2舍入到4的倍数

每个ArrayList将是24 +容量* 4(我认为) 。显然,IDMap将取决于实现。


那么,如何退出您的估计实际上取决于字符串的大小

和ArrayLists的容量。你能给一个样品和

你看到的大小,我们会看看能否解释一下?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


请记住,C#中的字符串是宽字符串,所以你需要

最小2 *长度。您需要添加像Length这样的数据成员(

我确定每次访问Length属性时都不会重新计算)和

内部成员来攻击字符串的地址,加上每个字符至少2

字节。即使在C ++中,字符串对象也很重。


我不知道的ArrayLists。我想这取决于对象的内存和对象的管理方式以及它的编码方式(性能与大小)再次

请记住内部成员,如大小,参考,和synconizer

对象。还有什么列表存储?容量* 4让我假设

参考类型,但如果它们是价值类型或结构,它可以更大。


..02

jim


" Mikolas" <弥***** @ discussions.microsoft.com>在消息中写道

新闻:FE ********************************** @ microsof t.com ...

例如,这些数据成员中的一个类:
{
uint sectionID; //我的估计4B
string sectionName; // dunno,但希望不超过2 *长度

ArrayList小节; //我的估计10B +容量* 4
ArrayList机器; //我估计10B +容量* 4
ArrayList graphicObjs; //我的估计10B +容量* 4
IDMap graphicObjsToSubsections; //我的估计10B +容量* 4
IDMap graphicObjsToMachines; //我估计10B +容量* 4

mikolas

Jon Skeet [C#MVP]"写道:

Mikolas< Mi ***** @ discussion.microsoft.com>写道:

我正在编写一个在.NET和Compact Frameword下运行的应用程序。我需要分配很多小内存块。它们形成一棵n树,即彼此指向的树。该程序的内存分配只是一个充满意义的东西 - 它比我估计应该采取的这些对象大约多4倍。在PC上它并不是什么大问题,但在PPC 32MB设备上这是一个非常大的问题。有谁知道它有什么可以做的吗?或者为什么会发生这种情况?



您能举例说明您要分配的对象类型,以及如何估算所需的内存使用量?

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



Hello,

I''m writing an app running both under .NET and Compact Frameword. I need to allocate lot of little mem blocks. They form a n-ary tree, i.e. pointing at each other. The mem alloc by the program is just prepostrous - it''s about 4times more than I esimate these objects should take. On PC it''s not such a big deal but on the PPC 32MB device it''s quite a problem. Does anyone know it there''s something to do about it? Or why it is happening?

tnhx

解决方案

Mikolas <Mi*****@discussions.microsoft.com> wrote:

I''m writing an app running both under .NET and Compact Frameword. I
need to allocate lot of little mem blocks. They form a n-ary tree,
i.e. pointing at each other. The mem alloc by the program is just
prepostrous - it''s about 4times more than I esimate these objects
should take. On PC it''s not such a big deal but on the PPC 32MB
device it''s quite a problem. Does anyone know it there''s something to
do about it? Or why it is happening?



Could you give an example of the kind of object you''re allocating, and
how you''re estimating the required memory usage?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Mikolas <Mi*****@discussions.microsoft.com> wrote:

For example one of the classes these data members :
{
uint sectionID; // my estimate 4B
string sectionName; // dunno, but hope not much more than 2*length

ArrayList subsections;//my estimate 10B + capacity*4
ArrayList machines;//my estimate 10B + capacity*4
ArrayList graphicObjs;//my estimate 10B + capacity*4
IDMap graphicObjsToSubsections;//my estimate 10B + capacity*4
IDMap graphicObjsToMachines;//my estimate 10B + capacity*4



Okay. For one thing, there''s 8 bytes overhead for the class itself.
Then for each reference, there''s a 4 byte overhead. string takes
12 bytes + (length+1)*2 rounded up to a multiple of 4
Each ArrayList is going to be 24 + capacity*4 (I think). IDMap will
depend on the implementation, obviously.

So, how "out" your estimates are really depend on the size of strings
and the capacity of your ArrayLists. Could you give a sample object and
what size you''re seeing, and we''ll see if we can explain it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Keep in mind that strings in C# are wide character strings, so there''s you
minimum 2*length. The you need to add in data members like Length (which
I''m sure is not recaclulated everytime the Length property is accessed) and
the internal member to stroe the address of the string, plus a minimum of 2
bytes per character. Even in C++ string objects can be heavy.

The ArrayLists I don''t know about. I guess it depends on the memory
management of the object and how it was coded(performance vs. size) Again
keep in mind internal members such as size, reference, and synconizer
objects. Also what are the lists storing? Capacity * 4 makes me assume
refernce types but if they are value types or structs it can be larger.

..02
jim

"Mikolas" <Mi*****@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...

For example one of the classes these data members :
{
uint sectionID; // my estimate 4B
string sectionName; // dunno, but hope not much more than 2*length

ArrayList subsections;//my estimate 10B + capacity*4
ArrayList machines;//my estimate 10B + capacity*4
ArrayList graphicObjs;//my estimate 10B + capacity*4
IDMap graphicObjsToSubsections;//my estimate 10B + capacity*4
IDMap graphicObjsToMachines;//my estimate 10B + capacity*4

mikolas

"Jon Skeet [C# MVP]" wrote:

Mikolas <Mi*****@discussions.microsoft.com> wrote:

I''m writing an app running both under .NET and Compact Frameword. I
need to allocate lot of little mem blocks. They form a n-ary tree,
i.e. pointing at each other. The mem alloc by the program is just
prepostrous - it''s about 4times more than I esimate these objects
should take. On PC it''s not such a big deal but on the PPC 32MB
device it''s quite a problem. Does anyone know it there''s something to
do about it? Or why it is happening?



Could you give an example of the kind of object you''re allocating, and
how you''re estimating the required memory usage?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于大内存开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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