How to increase memory to avoid exception std::bad_alloc?
-
As can you see in the code I used delete[], I used also delete only and in release mode but the memory was not freed. By using static allocation my program used 26MB only. I tried this simple code and worked fine:
#include
void main()
{
long i,size=((long)(1024L*1024L*50L/6L))*6;
for (i=0;i<100;i++)
{
__int16 (*memory)[6]=new __int16[1024L*1024L*50L/6L][6];
memory[1024L*1024L][5]=132;
delete memory;
printf("\nAllocated and deallocated %li MB",i*(size/1024L)*sizeof(__int16)/1024);
}
printf("\n=== FIN ===");
getchar();getchar();
}Perhaps the visual studio could not deallocate the memory to be reused because any operation reason
I saw that you used the array form there. My suggestions was to check it elsewhere in your program. When checking it again I saw that you use this:
delete[] filtro2,filtro3;
Asking myself if this is allowed, I searched and found that it is allowed but is a so called placement delete which effectively does nothing. Use this instead and try your original code:
delete[] filtro3;
delete[] filtro2; -
I made a image comparison program, but an exception error appeared at iteration number 1350, the code is here:
void compara_face(__int16 cara1[YMAX][XMAX],__int16 centro1[2*3],__int16 cara2[YMAX][XMAX],__int16 centro2[2*3],__int16 zoom_level,__int16 num_filtros,double error[2])
{
error[0]=error[1]=1e199;//error en direccion x e y
if (zoom_level >DMAX) { printf("\nERROR en compara_face() zoom_level =%i > %i",zoom_level ,DMAX);return; }
if (num_filtros>FMAX) { printf("\nERROR en compara_face() num_filtros=%i > %i",num_filtros,FMAX);return; }
double coef[6];//coeficiente de trasformada de 3 puntos
trasformada3p_hallacoef(centro1,centro2,coef);
__int16 (*filtro2)[6]=new __int16[num_filtros][6];
__int16 (*filtro3)[6]=new __int16[num_filtros][6]; ERROR IS HERE!!!!
__int16 d=DELTA[zoom_level],z=zoom_level;//delta de x e y
long i;__int16 x1,y1;//direccion x FILTROX\[DMAX\]\[FMAX\]\[2\]; transformada3P\_dx(coef,FILTROX\[z\],filtro2,d,num\_filtros); transformada3P\_dy(coef,FILTROY\[z\],filtro3,d,num\_filtros); error\[0\]=error\[1\]=0.0;//error en direccion x e y for (i=0;i
The error appear in the second "new" command. The program uses a lot of RAM, perhaps I need to compiler or linker to use more ram or allocate memory as static instead dynamic.
The program uses about 2 gigabytesI tried with VS2008 and it fails to allocate more than 2GB becaus delete command does not free memory. Using VS2015 (x86) fails also Using VS2015 x64 fails but more than 4GB are allocated
-
I saw that you used the array form there. My suggestions was to check it elsewhere in your program. When checking it again I saw that you use this:
delete[] filtro2,filtro3;
Asking myself if this is allowed, I searched and found that it is allowed but is a so called placement delete which effectively does nothing. Use this instead and try your original code:
delete[] filtro3;
delete[] filtro2;YES, you are right Thank you very much, you solved me a lot of problems
-
YES, you are right Thank you very much, you solved me a lot of problems
I think we have both learned something new today (I did not know the placement delete before) :)
-
Thank you Jochen. I used the windows task manager and I found that the
delete
command does not frees memory. I changed to static allocation and the program run. I used a VS2008 sp1, but perhaps I have to add any hotfixes. If someone is interested in the problem I can try also in VS2015 to see what happensMember 11988283 wrote:
I used the windows task manager and I found that the
delete
command does not frees memory.The numbers you see in Task Manager are almost, but not quite completely, useless for telling how much of your program's memory is in use. If you see it shrink, fine, but if you don't, it is not necessarily bad.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I think we have both learned something new today (I did not know the placement delete before) :)
More than the two of you learned something new. :thumbsup:
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Thank you Jochen. I used the windows task manager and I found that the
delete
command does not frees memory. I changed to static allocation and the program run. I used a VS2008 sp1, but perhaps I have to add any hotfixes. If someone is interested in the problem I can try also in VS2015 to see what happenstask manager isn't going to help you much. at a high level, it works something like this: when you allocate memory, it comes from your process's heap - if that heap is too small, it allocates more from the OS (up to the OS's per-process memory limit). when you free memory, it typically goes back to the process's heap, not back to the OS. this happens because it's common for a program to allocate/free the same amount of memory over and over, and it's quicker to just grab memory from the process's heap than to get the OS involved. so, under normal situations, your heap rarely shrinks. it typically grows and grows until it gets big enough to keep your program satisfied. it's only when your app shuts down that the heap memory is finally and completely released back to the OS. if the OS itself is running low on memory, it will start grabbing unused heap memory from your process. so sometimes you'll see Task Manager show a drop after a large free - some other process needs a large contiguous block and the OS hands it over. but don't count on that.
-
I saw that you used the array form there. My suggestions was to check it elsewhere in your program. When checking it again I saw that you use this:
delete[] filtro2,filtro3;
Asking myself if this is allowed, I searched and found that it is allowed but is a so called placement delete which effectively does nothing. Use this instead and try your original code:
delete[] filtro3;
delete[] filtro2; -
More than the two of you learned something new. :thumbsup:
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
About using the task manager>process window, I found that there is an error of memory used varies about 4k from one run to the following, so it can be used to see memory variations in debug sessions taken in account that. I used it to run the following very simple code:
#include void program_good()
{ //step #2
char *memory=new char[10000L*1024L];
memory[1024]=' '; //step #3
delete[] memory;
} //step #4void program_bad()
{
char *memory=new char[10000L*1024L];
memory[1024]=' ';// step #6
}void main()
{
double x[10];char ptr0\[1024\];// step #1 program\_good(); char ptr1\[1024\]; printf("\\nMemory diference=%li",(long) (&ptr1-&ptr0-1024)); program\_bad();// step #5 char ptr2\[1024\]; printf("\\nMemory diference=%li",(long) (&ptr2-&ptr1-1024)); printf("\\n=== FIN ===");//step #6 getchar();getchar();
In the task manager I found that my "Prueba.exe" memory usage was: step #1 and #2: 484k step #3: 10508k step #4: 484k step #5: 492k (I do not know why printf used 8kb) step #6: 10516k As result the calling of the program_bad() leaks 10Mb plus 32kb So the task manager is not an exact tool but can help
-
About using the task manager>process window, I found that there is an error of memory used varies about 4k from one run to the following, so it can be used to see memory variations in debug sessions taken in account that. I used it to run the following very simple code:
#include void program_good()
{ //step #2
char *memory=new char[10000L*1024L];
memory[1024]=' '; //step #3
delete[] memory;
} //step #4void program_bad()
{
char *memory=new char[10000L*1024L];
memory[1024]=' ';// step #6
}void main()
{
double x[10];char ptr0\[1024\];// step #1 program\_good(); char ptr1\[1024\]; printf("\\nMemory diference=%li",(long) (&ptr1-&ptr0-1024)); program\_bad();// step #5 char ptr2\[1024\]; printf("\\nMemory diference=%li",(long) (&ptr2-&ptr1-1024)); printf("\\n=== FIN ===");//step #6 getchar();getchar();
In the task manager I found that my "Prueba.exe" memory usage was: step #1 and #2: 484k step #3: 10508k step #4: 484k step #5: 492k (I do not know why printf used 8kb) step #6: 10516k As result the calling of the program_bad() leaks 10Mb plus 32kb So the task manager is not an exact tool but can help
Good to know, but I was refering more to Jochen's 'discovery' of placement delete.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle