c ++内存优化 [英] c++ memory optimization

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

问题描述




我有一个可以创建数十万个对象的c ++程序。

最后,我们希望能够创造数百万,但记忆是限制我们的。
限制我们。例如,125,000个物体目前占用286兆美元的

内存。


这导致了几个问题...


首先,访问器功能占用空间吗?在设计课程时,我是否应该注意某些技巧?b $ b我假设最轻的

对象我只能使用只有必要成员的容器,这是对吗?


是否有任何书籍或好的网站可以帮助我减轻课程的费用

一点点?


谢谢,

布莱恩

解决方案

在文章< lg ******************* @ newssvr14.news。 prodigy.com> ,@ bc

说......



我有一个c ++程序,可以创建数十万个对象。
最终,我们希望能够创造数百万,但记忆限制了我们。例如,125,000个对象目前占用286兆的内存。

这导致了几个问题...

首先,访问器功能占用空间?


至少和通常实现的一样,成员函数(比如其他代码)只需要

占用一次空间,无论你的类的对象数量是多少br />
创建。

在设计课程时,我是否应该注意某些技巧?我假设我能使用的最轻的物品只是一个只有必要成员的容器,这是对吗?




它''对于这个问题可能不是一个有用的答案,但最轻的可能对象是一个空的基类(可以是零大小)。任何

其他对象必须占用至少一个字节,即使它没有数据。

作为一个容器之间没有特别强烈的关系

和轻量级 - 这是可能的(甚至可能)但是那个'b
就像你能说的一样强烈。


-

后来,

杰瑞。


宇宙是自己想象的虚构。


BCC写道:



我有一个可以创建数十万个对象的c ++程序。
最终,我们希望能够创造数百万,但记忆限制了我们。例如,125,000个对象目前占用286兆的内存。

这导致了几个问题...

首先,访问器功能占用空间?在设计课程时,我是否应该注意某些技巧?我假设我可以使用的最轻的物体只是一个只有必要成员的容器,这是对的吗?

是否有任何书籍或好的网站可以帮助我点亮我们的课程

谢谢,
布莱恩




您可能会对您的操作系统感到惊讶管理记忆。我以16字节的块分配了内存,因此我可以使用

标准分配器的最小对象是16字节。


如果您的平台行为相似并且您预计对象需要少于16个字节,那么您可能会考虑为它们编写一个自定义的

分配器。


-Peter


" BCC" < a@b.c>在消息新闻中写道:< lg ******************* @ newssvr14.news.prodigy .com> ...



我有一个可以创建数十万个对象的c ++程序。
最终,我们希望能够创造数百万个,但是内存限制了我们。例如,125,000个对象目前占用286兆的内存。

这导致了几个问题...

首先,访问器功能占用空间?


函数占用空间,但它们很小,每个

类而不是每个对象只出现一次。

有没有在设计课程时我应该注意哪些技巧?我假设我可以使用的最轻的物体只是一个只有必要成员的容器,这是对的吗?

是否有任何书籍或好的网站可以帮助我稍微减轻我们的课程


谢谢,
布莱恩




我能想到的唯一真正一般的东西是看你的班级

是否有虚拟功能,若然,他们是否需要它们。如果你不需要b $ b需要它们,那就去除它们吧。它将释放

vtable所占用的几个字节,这是基于每个对象的。当然,这样做通常不是一个选项。


除此之外,我只想说看看你的课程试图剥离

你不需要的任何东西。你有非常大的物品(每个物品都是两块钱),所以可能会切掉一些东西。


Evan


Hi,

I have a c++ program that creates hundreds of thousands of objects.
Eventually, we would like to be able to create millions, but memory is
limiting us. For example, 125,000 objects currently takes up 286 megs of
memory.

This leads to a couple questions...

First, do accessor functions take up space? Are there certain tricks that I
should be aware of when designing a class? I am assuming the lightest
object I can use is just a container with only the necessary members, is
this right?

Are there any books or good websites that will help me lighten our classes
up a bit?

Thanks,
Bryan

解决方案

In article <lg*******************@newssvr14.news.prodigy.com> , a@b.c
says...

Hi,

I have a c++ program that creates hundreds of thousands of objects.
Eventually, we would like to be able to create millions, but memory is
limiting us. For example, 125,000 objects currently takes up 286 megs of
memory.

This leads to a couple questions...

First, do accessor functions take up space?
At least as usually implemented, member functions (like other code) only
take up space once, regardless of the number of objects of the class you
create.
Are there certain tricks that I
should be aware of when designing a class? I am assuming the lightest
object I can use is just a container with only the necessary members, is
this right?



It''s probably not a useful answer to the question, but the lightest
possible object is an empty base class (which can have zero size). Any
other object must occupy at least one byte, even if it has no data.
There''s no particularly strong relationship between being a container
and being lightweight -- it''s possible (maybe even likely) but that''s
about as strongly as you can put it.

--
Later,
Jerry.

The universe is a figment of its own imagination.


BCC wrote:

Hi,

I have a c++ program that creates hundreds of thousands of objects.
Eventually, we would like to be able to create millions, but memory is
limiting us. For example, 125,000 objects currently takes up 286 megs of
memory.

This leads to a couple questions...

First, do accessor functions take up space? Are there certain tricks that I
should be aware of when designing a class? I am assuming the lightest
object I can use is just a container with only the necessary members, is
this right?

Are there any books or good websites that will help me lighten our classes
up a bit?

Thanks,
Bryan



You might be surprised at how your OS manages memory. Mine allocates
memory in 16 byte chunks, so the smallest object I can have with the
standard allocator is 16 bytes.

If your platform behaves similarly and you anticipate having objects
that need less than 16 bytes, you might look into writing a custom
allocator for them.

-Peter


"BCC" <a@b.c> wrote in message news:<lg*******************@newssvr14.news.prodigy .com>...

Hi,

I have a c++ program that creates hundreds of thousands of objects.
Eventually, we would like to be able to create millions, but memory is
limiting us. For example, 125,000 objects currently takes up 286 megs of
memory.

This leads to a couple questions...

First, do accessor functions take up space?
Functions take up space, but they are small and only occur once per
class rather than per object.
Are there certain tricks that I
should be aware of when designing a class? I am assuming the lightest
object I can use is just a container with only the necessary members, is
this right?

Are there any books or good websites that will help me lighten our classes
up a bit?

Thanks,
Bryan



The only thing really general I can think of is to see if your classes
have virtual functions, and if so, if they need them. If you don''t
need them, get rid of them. It will free up the few bytes taken by the
vtable, which is a per-object basis. Of course, doing this is often
not an option.

Other than that, I''d just say look through your class to try to strip
out anything you don''t need. You''ve got really really big objects (a
couple KB each), so it might be possible to cut out something.

Evan


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

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