C ++中用于特征提取的内存分配问题 [英] Problem in Memory Allocation in C++ for feature extraction

查看:54
本文介绍了C ++中用于特征提取的内存分配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual C(win32控制台应用程序)进行功能提取程序.在我的项目中,我将从经过训练的图像中提取特征.特征不是结构形式. Strcuture具有4个DOUBLE数据项和1个大小为128的INT数组.因此,这意味着图像中有一个功能.平均而言,一张图像可以从500-1000个特征中生成.

我正在使用动态内存分配来存储每个训练图像的结构.我正在使用800张图像进行训练.这意味着要在RAM中保留这些功能,将消耗大量内存.现在的问题是,在训练了740张图像之后,我遇到了内存泄漏的错误,即无法分配足够的内存.我尝试了所有可能的方法来避免它,但仍然无法解决问题.所以,请告诉我如何克服这个问题.我将非常感激.

I am using Visual C (win32 Console Application) for feature extraction program. In my project I am extracting features from the trained images. Features aren in form of structures. Strcuture has 4 DOUBLE data items and 1 INT array of size 128. So this means one feature from the image. On average one image may generate from 500-1000 features.

I am using dynamic memory allocation to store the structures for every trained image. I am using 800 images for training. This means huge amount of memory will be consumed to keep these features iN RAM. Now problem is that after training of 740 images I am getting error of memory leak i.e. insufficient memory to allocate. I have tried every possible way to avoid it but still cannot solve the issue. So please tell me how i can overcome this issue. I will be really grateful.

推荐答案

内存泄漏是指您在分配内存后的某个时候不释放内存.

您以大约0.5k的结构分配了约800个结构(给予或大喊大叫),因此应该只有大约400K.如今,这已不是很大的内存(除非您使用某种嵌入式系统,在这种情况下,动态内存分配可能是不行的).我希望您能够创建〜2000倍并摆脱它.

那么您要为此分配多少内存呢?
A memory leak is when you don''t free memory at some point after you allocate it.

You''ve allocated ~800 structures at about 0.5k a structure (give or take some shouting) so that should only be about 400K. These days this isn''t a large amount of memory (unless you''re using some sort of embedded system in which case dynamic memory allocation is probably a no-no). I''d have expected you to be able to create ~2000 times that and get away with it.

So which way are you allocating memory for this lot?


我收到内存泄漏错误,即内存不足,无法分配.

它们是两个不同的东西:
内存泄漏"是指您释放分配的内存块的可及性,以便在不再需要它时无法释放它.
大量内存泄漏可能会导致内存不足.
有时-总而言之-内存不足的情况可能会导致某些兴奋/退出,导致过早的函数退出,从而导致某些内存丢失,从而导致泄漏.

通常,这两个问题都是不良设计"的症状,很少可以解决为待解决的问题".
您必须更好地设计管理内存分配/释放的方式,正弦很有可能实数"比您谈论的数字大得多.
I am getting error of memory leak i.e. insufficient memory to allocate.

They are two different thing:
A "memory leak" is when you loose the reachability of a memory block you allocate, so that you cannot free it when it is anymore need it.
Large amount of memory leaks can cause insufficient memory condition.
Sometimes -viceversa- an insufficient memory condition may throw some excetion/cause premature function exiting, so that some memory is lost somewhere causing leaks.

In general both problem are symptoms of "bad design", and are rarely solvable as "problem to be fixed".
You''ve have to better design the way memory allocation/release is managed, sine is highly probable that "real" numbers are much and much more than the ones you talked about.


I建议您将文件直接映射到内存中,然后只需将指针类型转换为指向结构的指针即可.然后索引这个数组就很简单了.

这可能是最快的速度,而且您不必分配/取消分配任何内存.

概念的示例:
I recommend you map the file directly into memory, and just type cast the pointer to a pointer to your struct. Then it''s a small deal indexing this array.

This is probably as fast as it gets, and you don''t have to allocate/deallocate any memory.

Example of the concept:
struct Feature
{
    double d1, d2, d3, d4;
    int arr[128];
};

HANDLE hFile = ::CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);

size_t nFileSize = ::GetFileSize(hFile, NULL);
HANDLE hMem = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);

Feature *pFeatures = (Feature*)::MapViewOfFile(hMem, FILE_MAP_READ, 0, 0, 0);

size_t featureCount = nFileSize / sizeof(Feature);

for (size_t f = 0; f < featureCount; ++f)
{
    Feature *pFeature = &pFeatures[f];
    // Use pFeature here
    // As long as you have the file mapping open, you can use the pointers.
}


错误检查和文件映射关闭被省略.

您可能需要根据数据在Feature结构中对齐成员.但是现在不一样了.

较小的代码更改


Error checking, and closing of the file mapping, omitted.

You might need to align the members in the Feature struct depending on your data. But not as it is right now.

Minor code changes


这篇关于C ++中用于特征提取的内存分配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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