简体   繁体   English

释放分配的内存

[英]Freeing allocated memory

Is this good practice? 这是好习惯吗? Or should I just replace the code block between { and } with a function? 或者我应该用函数替换{}之间的代码块? It could be reusable (I admit) but my only motivation for doing this is to deallocate colsum since it's huge and not required so that I can free the allocated memory. 它可以重复使用(我承认),但我这样做的唯一动机是释放colsum因为它很大而且不是必需的,所以我可以释放分配的内存。

 vector<double> C;
 {
  vector<double> colsum;
  A.col_sum(colsum);
  C = At*colsum;
 }
 doSomething(C);

在我的书中使用括号来调整自动变量的范围很好,但通常如果你发现自己做了很多,特别是在同一个函数中多次,你的函数可能做了太多不同的事情,应该分解。

The data for vectors is always dynamically allocated. 矢量的数据总是动态分配的。 Only the bookkeeping data is stored on the stack. 只有簿记数据存储在堆栈中。 Even if it weren't, stack memory allocation is essentially free. 即使不是,堆栈内存分配基本上是免费的。 Deallocating from the stack is just changing the value of a register on most architectures. 从堆栈中取消分配只是在大多数体系结构上更改寄存器的值。

EDIT 编辑

Regarding the dynamic deallocation, it will have to be deallocated at one point or another (particularly the end of the function). 关于动态解除分配,必须在某一点或另一点(特别是功能的结束)解除分配。 You're not actually losing anything by leaving the memory allocated until you want to allocate more and there isn't enough. 你实际上没有丢失任何东西,只需要分配内存,直到你想分配更多而且还不够。 Is the precise timing of when that deallocation occurs really something to be concerned about before you actually run into some problem? 在实际遇到某些问题之前,解除分配的确切时间是否真的需要关注?

/EDIT /编辑

But what's the point? 但重点是什么? It really seems like you're prematurely concerning yourself with optimization. 看起来你似乎过早地关注自己的优化问题。

If you want to refactor your code, do it for the sake of clarity, not performance. 如果要重构代码,请为了清晰起见而不是性能。

Vector doesn't store memory on the stack. Vector不会在堆栈中存储内存。 Only the vector object itself is stored there, which is not very large. 只有矢量对象本身存储在那里,这不是很大。 Scoping it like this does force a destruction which will free the memory it has allocated. 像这样确定它会强制破坏,这将释放它已分配的内存。

Also, I'm not sure that it's specified in the ISO that an implementation must pop a subscope variable off of the stack. 此外,我不确定在ISO中指定实现必须从堆栈中弹出子范围变量。

As other have pointed out, the vector memory is not allocated in the stack. 正如其他人所指出的那样,向量存储器没有在堆栈中分配。 If you want to free that memory early on, the common idiom is: 如果你想早点释放那段记忆,那么常见的习语就是:

vector<double> C;
vector<double> colsum;
A.col_sum(colsum);
C = At*colsum;
    std::vector<double>().swap(colsum);
doSomething(C);

That will create a temporary vector and swap the contents with your large vector. 这将创建一个临时向量并将内容与您的大向量交换。 At the end of the instruction the temporary will be destroyed and the memory released. 在指令结束时,临时将被销毁并释放内存。 You will be left with an empty vector. 你将留下一个空的向量。

Note that colsum.resize(0) and colsum.clear() don't need to free the available memory, and in many cases they won't assuming that if the vector grew to that size before, chances are that it will do it again. 请注意, colsum.resize(0)colsum.clear()不需要释放可用内存,并且在许多情况下他们不会假设如果向量增长到那个大小之前,很可能它会这样做再次。

If the inner code will be reused elsewhere, separate it into a function. 如果内部代码将在别处重用,请将其分离为函数。 If the inner code is called frequently (like in a loop), then it probably needs to be refactored so that the vector isn't being constantly created and destroyed in a loop. 如果频繁调用内部代码(如在循环中),则可能需要重构内容,以便不在循环中不断创建和销毁向量。 Otherwise, I don't think it is bad practice to do what you've suggested. 否则,我认为按照你的建议去做是不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM