9 years, 8 months ago.

Different behaviors of New operator - Dynamic memory allocation

Hi everyone, i tested an sample try-catch block with a simple memory allocation program in an embed device which runs with only 64MB RAM with customized linux OS 32-bit Xscale Arm architecture.My intention was to verify whether a normal try catch block will work when there is no memory on this device.Im listing the code as below.Assume this as program1

#include "stdio.h"
#include "stdlib.h"
int main()
{
        int count;
        int *q[409600];
        printf("\nHeap Leak Starting at..");
        system("date");
        fflush(stdout);
        printf("\n, Count is located at ,%p,",&count);
        for(count=0;count<409600;count++)
        {
                printf("\nCount = ,%d,",count);
                int i=2;
                try
                {
                        fflush(stdout);
                        q[count] = new int[409600];
                        printf("New Success.Allocated %ld Bytes at %p.Difference = 0x%x,\n",(i*100000),q[count],(long)(&i) - (long)q[count]);
                        fflush(stdout);
                }
                catch(...)
                {
                                printf("\nException Caught...New Failed..No Memory Available.\n");
                                fflush(stdout);
                                exit(1);
                }
        }
}

Output of Program1:

Thu Jul 31 20:38:00 UTC 2014

Heap Leak Starting at..

, Count is located at ,0xbffffc48,

Count = ,0,New Success.Allocated 200000 Bytes at 0x402c0008.Difference = 0x7fbafc3c,

Count = ,1,New Success.Allocated 200000 Bytes at 0x40451008.Difference = 0x7fa1ec3c,

Count = ,2,New Success.Allocated 200000 Bytes at 0x405e2008.Difference = 0x7f88dc3c,

Count = ,3,New Success.Allocated 200000 Bytes at 0x40773008.Difference = 0x7f6fcc3c,

........The count goes on........

Count = ,1954,New Success.Allocated 200000 Bytes at 0xbf854008.Difference = 0x61bc3c,

Count = ,1955,New Success.Allocated 200000 Bytes at 0xbf9e5008.Difference = 0x48ac3c,

Count = ,1956,New Success.Allocated 200000 Bytes at 0xbfb76008.Difference = 0x2f9c3c,

Count = ,1957,

Exception Caught...New Failed..No Memory Available.

Now this is an expected behaviour.Since no memory is there,Exception is throwed.

Then i modified the line "q[count] = new int[409600]" to "q[count] = new int[100000]".Lets assume this as program2.I recompiled the program and executed on the same device.

Output of Program2:

Thu Jul 31 20:39:43 UTC 2014

Heap Leak Starting at..

, Count is located at ,0xbffffc48,

Count = ,0,New Success.Allocated 200000 Bytes at 0x402c0008.Difference = 0x7fbafc3c,

Count = ,1,New Success.Allocated 200000 Bytes at 0x40322008.Difference = 0x7fb4dc3c,

Count = ,2,New Success.Allocated 200000 Bytes at 0x40384008.Difference = 0x7faebc3c,

Count = ,3,New Success.Allocated 200000 Bytes at 0x403e6008.Difference = 0x7fa89c3c,

............The count goes on.............

Count = ,5215,New Success.Allocated 200000 Bytes at 0x87d00018.Difference = 0x3816fc2c,

Count = ,5216,New Success.Allocated 200000 Bytes at 0x87d61aa0.Difference = 0x3810e1a4,

Count = ,5217,New Success.Allocated 200000 Bytes at 0x87e00018.Difference = 0x3806fc2c,

Count = ,5218,New Success.Allocated 200000 Bytes at 0x87e61aa0.Difference = 0x3800e1a4,

Count = ,5219,New Success.Allocated 200000 Bytes at 0x87f00018.Difference = 0x37f6fc2c,

Count = ,5220,

And at this point,the process gets hanged forever at the New operator.I could not access the terminal even if i run the process in background.

Now im confused on noticing this different behaviour of "new".Please note that in the exception throwing case im allocating a large size(409600) than the case where the process gets hanged(100000). Even this could be root cause for the Ui process hang issue which was reported So,a breakthrough for this sample program's behaviour would show the direction for breaking the ui process hang. Can you help me in clarifying this different behvaiour of New? Pls share your thoughts and some clues..

PS:Ignore the number "200000" as i have printed it wrongly.

Just a thought but maybe it needs a certain amount of free memory just to be able to report back. If the failed "new" happens to return a big chunk of memory it carries on. If it is allocating smaller "chunks" then maybe the memory left is insufficient for some critical operation so it hangs.

posted by Oliver Broad 26 Apr 2016

1 Answer

9 years, 8 months ago.

Take into account this site is for people who use mbed devices. The specific issues with new that were in the other thread are somewhere due to mbed internals. Your problem with linux would probably be better answered on a forum/area (stackexchange maybe?) where people use those.

Hi Erik, Ya i was aware of that when i posted this question.. since i found a similar question in this mbed site..i posted here.. just to get some thought and ideas.. sorry if i was wrong..

posted by Arunkumar Ravi 01 Aug 2014

If it stays limitted I guess it isn't much of an issue, it is more that you might get better help in areas where people are experienced with your setup.

From what I quickly glanced is that mainly the question is why the second one stops. The first one is obvious: It runs out of memory. But the second one still has plenty of memory left. So why does it stop functioning? What happens if you half the allocated size again?

posted by Erik - 01 Aug 2014