This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Checking for memory usage on runtime

Hey everyone,

I'm having problems with my app's ram usage as you can see from my previous questions.

I've tried this little piece of code:

void memcheck(void){
	// perform free memory check
	int blockSize = 16;
	int i = 1;
	debug("Checking memory with blocksize %d char ...\n", blockSize);
	while (true) {
		char *p = (char *) malloc(i * blockSize);
		if (p == NULL){
			break;
		}
		free(p);
		++i;
	}
	debug("Ok for %d char\n", (i - 1) * blockSize);
}

That I found here to test for free memory.

I get this output:

Test for free memory Checking memory

with blocksize 16 char ...

Ok for 4752 char

And then my application just breaks. I've checked and it seems to me that the memory that was mallock'd should be freed, so I don't understand where this is wrong. What's more interesting is that this behavior is eerily similar to the behavior I get when I try running the lwip stack alongside my app (see here and here).

Can you guys help me understand what is going on here?

Parents
  • Cortex M0 does not generate hardfaults for this, this is normal memory corruption. MPU will allow it, it does not have any checks for HEAP/stack overflow worst thing is, you'll most likely grow into your globals as well. Regarding your question regarding some kind of warning, compilers should be able to figure out that heap memory is over allocated but stack is only runtime so compilers have no way to find that out. I cannot find any standard definition for compiler behavior for this. Maybe someone else can comment on that.

Reply
  • Cortex M0 does not generate hardfaults for this, this is normal memory corruption. MPU will allow it, it does not have any checks for HEAP/stack overflow worst thing is, you'll most likely grow into your globals as well. Regarding your question regarding some kind of warning, compilers should be able to figure out that heap memory is over allocated but stack is only runtime so compilers have no way to find that out. I cannot find any standard definition for compiler behavior for this. Maybe someone else can comment on that.

Children
No Data
Related