问:正确分配内存 [英] Q: Allocating memory correctly

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

问题描述




每当使用operator new或operator new []分配内存时,我觉得

就像我应该非常谨慎地使用它,因为否则内存浪费了

(管理所有这些分配所需的额外开销),这也是
失去一些性能。我特别谈到许多小的

分配(大约16-512字节)。我的理由是否合理?


谢谢!

-

jb


(如果你想通过电子邮件回复,用x替换y)

Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

thanks!
--
jb

(replace y with x if you want to reply by e-mail)

推荐答案



" Jakob Bieling <是ne ***** @ gmy.net>在消息中写道

新闻:bq ************* @ news.t-online.com ...
Hi,
"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bq*************@news.t-online.com...
(管理所有这些所需的额外开销)分配)也失去了一些表现。我特别谈到许多小的分配(大约16-512字节)。我的理由是否合理?

通常新手必须搜索足够大的空闲块。自动

变量通常更快(因为它只是堆栈上的一些空间)。

尝试尽可能多地使用堆栈中的变量。对于变量来说,

会占用大量空间,或者你只需​​要动态创建

动态分配。


如果它会减慢您的程序速度,您可以随时使新操作员超负荷运行。

可以实现更智能(并且考虑到内存可能更浪费)。


问候,Ron AF格雷夫。

谢谢!
-
jb

(如果你想通过电子邮件回复,用x替换y)
Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?
Typically new has to search for a free block that is large enough. Automatic
variables are usually faster (because it is just some space on the stack).
Try to use variables on the stack as much as possible. For variables that
take up a lot of space or that you just have to create dynamically use
dynamic allocation.

If it slows down your program you can always overload the new operator and
make a smarter (and likely more wasteful considering memory) implementation.

Regards, Ron AF Greve.
thanks!
--
jb

(replace y with x if you want to reply by e-mail)



" Moonlit" <人****** @ jupiter.universe>在留言中写道

新闻:3f ********************* @ news.xs4all.nl ...
"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
每当使用operator new或operator new []分配内存时,I
感觉我应该非常谨慎地使用它,因为否则会浪费内存
Whenever allocating memory using operator new or operator new[] I feel like I should only use it very sparingly, because otherwise memory is wasted
(通过管理所有这些分配所需的额外开销),
也会失去一些性能。我特别谈到许多小的分配(大约16-512字节)。我的理由是否合理?
(by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?


通常新的必须搜索足够大的空闲块。


Typically new has to search for a free block that is large enough.



自动变量通常更快(因为它是只是堆栈上的一些空间。
尝试尽可能多地使用堆栈上的变量。对于占用大量空间的变量或者你必须动态创建动态分配的变量。


Automatic variables are usually faster (because it is just some space on the stack).
Try to use variables on the stack as much as possible. For variables that
take up a lot of space or that you just have to create dynamically use
dynamic allocation.






在我的情况下我不能使用自动变量,因为分配是

是一个未知大小的数组(但至少有1个元素)或变量

需要比自动变量更长寿命。但是由于这些分配很多可能会完成,我问自己是否有必要自己写一个内存管理器或者使用很多vector'的帮助(不确定

关于它的分配器,但我猜/希望它使用相同的内存

池用于相同类型的向量?)或者如果一切都很好方式我有

它。


谢谢

-

jb


(如果你想通过电子邮件回复,用x替换y)



Hi,

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would. But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself or if using many vector''s helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?) or if everything is fine the way I have
it.

thanks
--
jb

(replace y with x if you want to reply by e-mail)





Jakob Bieling <是ne ***** @ gmy.net>在消息中写道

news:bq ************* @ news.t-online.com ...
Hi,

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bq*************@news.t-online.com...
" Moonlit" <人****** @ jupiter.universe>在消息中写道
新闻:3f ********************* @ news.xs4all.nl ...
"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
每当使用operator new或operator new []分配内存时,我觉得我应该非常谨慎地使用它,因为否则会浪费内存
Whenever allocating memory using operator new or operator new[] I feel like I should only use it very sparingly, because otherwise memory is wasted
(需要额外的开销)管理所有这些分配)也失去了一些性能。我特别谈到许多小的分配(大约16-512字节)。我对这个
的理由是否合理?
(by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?


通常新的必须搜索足够大的空闲块。


Typically new has to search for a free block that is large enough.


自动

变量通常更快(因为它只是
堆栈上的一些空间)。尝试尽可能多地在堆栈上使用变量。对于占用大量空间的变量
,或者你只需​​要动态创建动态分配。
variables are usually faster (because it is just some space on the stack). Try to use variables on the stack as much as possible. For variables that take up a lot of space or that you just have to create dynamically use
dynamic allocation.




在我的情况下,我不能使用自动变量,因为分配是一个未知大小的数组(但至少有1个元素),或者变量
需要比自动变量更长寿命。但是由于很多这些分配可能会完成,我问自己是否有必要自己编写一个内存管理器,或者是否使用了很多vector'的帮助(不确定



Hi,

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would. But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself or if using many vector''s helps (not sure




我不会从那开始。

关于它的分配器,但我猜/希望它使用相同的内存
池同样的向量?)或者如果一切都很好,就像我b $ b那样。


如果我在哪里,我会使用vector<>对于数组。


然后,当你的应用程序实际运行得太慢并且分析

时,你的代码显示它是由于新的/删除你可能花一些时间在

重载new / delete运算符(不要释放类的内存但是

将它插入链表然后当一个新的完成后返回顶部

列表)。


问候,Ron AF Greve。

感谢
-
jb

(如果你想通过电子邮件回复,用x代替)



I wouldn''t start with that.
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?) or if everything is fine the way I have it.
If I where you I would use vector<> for arrays.

Then later on when your app actually does run too slow and an analysis of
your code shows it is due to new/delete you might invest some time in
overloading new/delete operator (do not release the memory for the class but
insert it into a linked list then when a new is done return the top of the
list).

Regards, Ron AF Greve.

thanks
--
jb

(replace y with x if you want to reply by e-mail)



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

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