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
  • app_timers use stack memory buffers and when you do malloc, the library creates more memory per block request to do book keeping. if heap allocation is affecting app_timers (which does not use heap) then the only reason i can see is that either malloc has moved into stack space or there was no enough stack space for app_timers and they used some heap space. decreasing heap_size and increasing stack size should solve your problem. Like Dan said you should change also change these in arm_startup_nrf51.s

Reply
  • app_timers use stack memory buffers and when you do malloc, the library creates more memory per block request to do book keeping. if heap allocation is affecting app_timers (which does not use heap) then the only reason i can see is that either malloc has moved into stack space or there was no enough stack space for app_timers and they used some heap space. decreasing heap_size and increasing stack size should solve your problem. Like Dan said you should change also change these in arm_startup_nrf51.s

Children
No Data
Related