C ++有效地使用新的运算符 [英] C++ Using the new operator efficiently

查看:164
本文介绍了C ++有效地使用新的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用新实例化一个类。而不是删除内存基于对象的重用将获得什么样的好处?

When instantiating a class with new. Instead of deleting the memory what kinds of benefits would we gain based on the reuse of the objects?

新的过程是什么?是否发生上下文切换?新内存被分配,谁在做分配? OS

What is the process of new? Does a context switch occur? New memory is allocated, who is doing the allocation? OS ?

推荐答案

您在这里提出了几个问题...

You've asked a few questions here...

而不是删除内存,我们将根据对象的重用获得什么样的好处?
Instead of deleting the memory what kinds of benefits would we gain based on the reuse of the objects?

这完全取决于你的应用程序。即使我知道应用程序是什么,你已经留下了另一个细节未指定 - 你的重用背后的战略是什么?但即使知道,很难预测或一般回答。尝试一些事情并测量它们。

That depends entirely on your application. Even supposing I knew what the application is, you've left another detail unspecified -- what is the strategy behind your re-use? But even knowing that, it's very hard to predict or answer generically. Try some things and measure them.

根据经验,我喜欢最小化最无益的分配。这主要是过早的优化,但。

As a rule of thumb I like to minimize the most gratuitous of allocations. This is mostly premature optimization, though. It'd only make a difference over thousands of calls.

新进程是什么?

完全实现依赖。但是分配器使用的一般策略是有一个空闲列表,即在进程中已经释放的块列表。当空闲列表为空或包含不足的连续可用空间时,它必须向内核询问内存,它只能在固定页面大小的块中给出。 (x86上的4096)。分配器还必须决定何时切断,填充或合并块。多线程也可以给分配器施加压力,因为它们必须同步它们的自由列表。

Entirely implementation dependent. But the general strategy that allocators use is to have a free list, that is, a list of blocks which have been freed in the process. When the free list is empty or contains insufficient contiguous free space, it must ask the kernel for the memory, which it can only give out in blocks of a constant page size. (4096 on x86.) An allocator also has to decide when to chop up, pad, or coalesce blocks. Multi-threading can also put pressure on allocators because they must synchronize their free lists.

通常这是一个相当昂贵的操作。也许没有那么多相对于你正在做什么。但它不便宜。

Generally it's a pretty expensive operation. Maybe not so much relative to what else you're doing. But it ain't cheap.

上下文切换是否发生?

完全可能。它也可能不会。你的操作系统可以随时做一个上下文切换,任何时候它得到一个中断或系统调用,所以呃...这可能发生在很多时候;我没有看到这和您的分配器之间的任何特殊的关系。

Entirely possible. It's also possible that it won't. Your OS is free to do a context switch any time it gets an interrupt or a syscall, so uh... That can happen at a lot of times; I don't see any special relationship between this and your allocator.

新内存分配,谁在做分配? OS?
New memory is allocated, who is doing the allocation? OS ?

它可能来自一个空闲列表,在这种情况下,不涉及系统调用,因此没有来自操作系统的帮助。但是如果空闲列表不能满足请求,它可能来自操作系统。此外,即使它来自于自由列表,你的内核可能已经分页了这些数据,所以你可以得到一个页面访问错误,内核的分配器会引入。所以我想这是一个混合的包。当然,你可以有一个符合的实现,做各种疯狂的事情。

It might come from a free list, in which case there is no system call involved, hence no help from the OS. But it might come from the OS if the free list can't satisfy the request. Also, even if it comes from the free list, your kernel might have paged out that data, so you could get a page fault on access and the kernel's allocator would kick in. So I guess it'd be a mixed bag. Of course, you can have a conforming implementation that does all kinds of crazy things.

这篇关于C ++有效地使用新的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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