1

I am having run-time memory allocation errors with a C++ application. I have eliminated memory leaks, invalid pointer references and out-of-bounds vector assignments as the source of the issue - I am pretty sure it's something to do with memory fragmentation and I am hoping to get help with how to further diagnose and correct the problem.

My code is too large to post (about 15,000 lines - I know not huge but clearly too large to put online), so I am going to describe things with a few relevant snippets of code.

Basically, my program takes a bunch of string and numerical data sets as inputs (class objects with vector variables of type double, string, int and bool), performs a series of calculations, and then spits out the resulting numbers. I have tested and re-tested the calculations and outputs - everything is calculating as it should, and on smaller datasets things run perfectly.

However, when I scale things up, I start getting memory allocation errors, but I don't think I am even close to approaching the memory limits of my system - please see the two graphs below...my program cycles through a series of scenarios (performing identical calculations under a different set of parameters for each scenario) - in the first graph, I run 7 scenarios on a dataset of about 200 entries. As the graph shows, each "cycle" results in memory swinging up and back down to its baseline, and the overall memory usage is tiny (see the seven small blips on the right half of the bottom graph). On the second graph, I am now running a dataset of about 10,000 entries (see notes on dataset below). In this case, I only get through 2 full cycles before getting my error (as it is trying to resize a class object for the third scenario). You can see the first two scenarios in the bottom right-half graph; a lot more memory usage than before, but still only a small fraction of available memory. And as with the smaller dataset, usage increases while my scenario runs, and then decreases back to it's initial level before reaching the next scenario.

This pattern, along with other tests I have done, lead me to believe it's some sort of fragmentation problem. The error always occurs when I am attempting to resize a vector, although the particular resize operation that causes the error varies based on the dataset size. Can anyone help me understand what's going on here and how I might fix it? I can describe things in much greater detail but already felt like my post was getting long...please ask questions if you need to and I will respond/edit promptly.

Clarification on the data set The numbers 200 and 10,000 represent the number of unique records I am analyzing. Each record contains somewhere between 75 and 200 elements / variables, many of which are then being manipulated. Further, each variable is being manipulated over time and across multiple iterations (both dimensions variable). As a result, for an average "record" (the 200 to 10,000 referenced above), there could be easily as many as 200,000 values associated with it - a sample calculation:

1 Record * 75 Variables * 150 periods * 20 iterations = 225,000 unique values per record.

SmallDataRun

LargeDataSetRun

Offending Code (in this specific instance):

vector<LoanOverrides> LO;
LO.resize(NumOverrides + 1); // Error is occuring here.  I am certain that NumOverrides is a valid numerical entry = 2985

// Sample class definition
class LoanOverrides {
public:
  string IntexDealName;
  string LoanID;
  string UniqueID;
  string PrepayRate;
  string PrepayUnits;
  double DefaultRate;
  string DefaultUnits;
  double SeverityRate;
  string SeverityUnits;
  double DefaultAdvP;
  double DefaultAdvI;
  double RecoveryLag;
  string RateModRate;
  string RateModUnits;
  string BalanceForgivenessRate;
  string BalanceForgivenessRateUnits;
  string ForbearanceRate;
  string ForbearanceRateUnits;
  double ForbearanceRecoveryRate;
  string ForbearanceRecoveryUnits;
  double BalloonExtension;
  double ExtendPctOfPrincipal;
  double CouponStepUp;
};
4

1 回答 1

3

您有一个能够分配大量内存的 64 位操作系统,但是将您的应用程序构建为 32 位应用程序,它最多只能分配大约 3GB 的内存。您正试图分配更多。

  • 尝试编译为 64 位应用程序。这可能使您能够实现目标。您可能需要增加页面文件的大小。
  • 看看您是否可以比当前更早地处理中间结果。
  • 尝试计算您的算法正在使用/将使用多少内存,并尝试重新设计您的算法以减少使用量。
  • 尝试通过重新设计算法来避免重复数据。我看到您有很多参考数据,从外观上看,这些参考数据在应用程序运行期间不会改变。您可以将所有这些放入一个向量中,分配一次,然后通过其他任何地方的整数索引引用它们,而不是复制它们。(只是猜测您正在复制它们)。
  • 尝试通过修改算法以批量处理来避免一次加载所有数据。

如果不了解您的应用程序,就不可能提供更好的建议。但基本上你的内存已经用完了,因为你分配了大量的内存,并且根据你的应用程序和你发布的代码片段,我认为你可以稍微考虑一下就可以避免这样做。祝你好运。

于 2013-09-05T16:34:56.357 回答