MBED Microcontroller Library Heap Stack Size

Hi Everyone,
I have an issue with “out of memory” error. From years ago we are usin Mbed Microcontroller Library and the ARM ToolChain for our project (LPC1768)but now we need to solve “out of memory” error because our board restart very frequently. I change some big buffers as static global variables and that works a little better. Then I need to check memory usage for heap and stack, Do you know how I can do that? Also I don’t know how to check the size of heap and stack during compilation.

Thanks for you help, best regards.

1 Like

I suggest checking out this article here: Tracking memory usage with Mbed OS | Mbed

That will get you started tracking the memory usage. Post back if you still need assistance.

1 Like

So now a couple of years later, I’m trying to follow the Tracking memory usage with Mbed OS | Mbed guide and video.

When I’m running the mbed-find-dangling-ptrs script it spits out that I have 2 dangling pointers of 111 bytes total. But the script does not really show where those are within the code, as it is advertised in the video. How could I find those pointers to evaluate if my program is sane?

I wanted to attach the .elf file and log if anyone would want to try and run it, but seems like i cant. However, as it is seen in the log file, I have confirmed with memory trace that heap size stays the same for multiple cycles of the program as you can see in the log snippet below.

Script output:

Extracting symbols OK

Found 2 dangling pointers (111 bytes)

-------------------------------------------------- 0x802ACF1
1 dangling pointers (total: 31 bytes): [ 0x20003D28 (31) ]
     802acda:	684d      	ldr	r5, [r1, #4]
     802acdc:	429a      	cmp	r2, r3
     802acde:	bf0c      	ite	eq
     802ace0:	220f      	moveq	r2, #15
     802ace2:	6882      	ldrne	r2, [r0, #8]
     802ace4:	42aa      	cmp	r2, r5
     802ace6:	d20a      	bcs.n	802acfe <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_assignERKS4_+0x34>
     802ace8:	a901      	add	r1, sp, #4
     802acea:	9501      	str	r5, [sp, #4]
>>>  802acec:	f7ff ff96 	bl	802ac1c <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERjj>
     802acf2:	4620      	mov	r0, r4
     802acf4:	f7ff ffb4 	bl	802ac60 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv>
     802acf8:	9b01      	ldr	r3, [sp, #4]
     802acfa:	6027      	str	r7, [r4, #0]
     802acfc:	60a3      	str	r3, [r4, #8]
     802acfe:	b125      	cbz	r5, 802ad0a <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_assignERKS4_+0x40>
     802ad00:	462a      	mov	r2, r5
     802ad02:	6831      	ldr	r1, [r6, #0]
     802ad04:	6820      	ldr	r0, [r4, #0]
     802ad06:	f7ff ffcb 	bl	802aca0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7_S_copyEPcPKcj>

-------------------------------------------------- 0x802C413
1 dangling pointers (total: 80 bytes): [ 0x20004410 (80) ]
    0802c3fc <strtok>:
     802c3fc:	4b13      	ldr	r3, [pc, #76]	; (802c44c <strtok+0x50>)
     802c3fe:	e92d 41f0 	stmdb	sp!, {r4, r5, r6, r7, r8, lr}
     802c402:	681f      	ldr	r7, [r3, #0]
     802c404:	4605      	mov	r5, r0
     802c406:	6dbc      	ldr	r4, [r7, #88]	; 0x58
     802c408:	460e      	mov	r6, r1
     802c40a:	b9b4      	cbnz	r4, 802c43a <strtok+0x3e>
     802c40c:	2050      	movs	r0, #80	; 0x50
>>>  802c40e:	f7ff fabb 	bl	802b988 <malloc>
     802c414:	e9c0 4400 	strd	r4, r4, [r0]
     802c418:	e9c0 4402 	strd	r4, r4, [r0, #8]
     802c41c:	e9c0 4404 	strd	r4, r4, [r0, #16]
     802c420:	e9c0 440a 	strd	r4, r4, [r0, #40]	; 0x28
     802c424:	e9c0 440c 	strd	r4, r4, [r0, #48]	; 0x30
     802c428:	e9c0 440e 	strd	r4, r4, [r0, #56]	; 0x38
     802c42c:	e9c0 4410 	strd	r4, r4, [r0, #64]	; 0x40
     802c430:	e9c0 4412 	strd	r4, r4, [r0, #72]	; 0x48
     802c434:	6184      	str	r4, [r0, #24]
     802c436:	7704      	strb	r4, [r0, #28]

Log file:

#m:0x20003CD0;0x8029239-160
#m:0x20004348;0x801C9D9-64
#m:0x20004398;0x801C9F1-64
#m:0x200043E8;0x801CA17-16
#m:0x20004408;0x801F1FF-12
#m:0x20004428;0x801E8B7-56
#f:0x0;0x801E8FB-0x20004428
#f:0x0;0x801F25D-0x20004408
#f:0x0;0x801C85D-0x20004348
#f:0x0;0x801C865-0x20004398
#f:0x0;0x801C86D-0x200043E8
#f:0x0;0x802984F-0x20003CD0
#m:0x20003D58;0x8028B25-21
#m:0x20003D28;0x802ACF1-31
#f:0x0;0x8029F51-0x20003D58
#m:0x200043D8;0x8028B25-131
#f:0x0;0x802A1E7-0x200043D8
#m:0x20004410;0x802C413-80
#m:0x20004360;0x8029239-160
#m:0x20003CD0;0x801C9D9-64
#m:0x20004470;0x801C9F1-64
#m:0x20003D58;0x801CA17-16
#m:0x200044C0;0x801F1FF-12
#m:0x200044D8;0x801E8B7-56
#m:0x20004520;0x801E24B-64
#m:0x20004570;0x802B937-1024
#f:0x0;0x801DD2B-0x20004520
#f:0x0;0x801E929-0x200044D8
#f:0x0;0x801F25D-0x200044C0
#f:0x0;0x802B093-0x20004570
#m:0x20004968;0x801F1FF-12
#m:0x20004920;0x801E8B7-56
#m:0x200048D0;0x801E24B-64
#m:0x200044C0;0x802B937-1024
#f:0x0;0x801DD2B-0x200048D0
#f:0x0;0x801E929-0x20004920
#f:0x0;0x801F25D-0x20004968
#f:0x0;0x802B093-0x200044C0
#f:0x0;0x801C85D-0x20003CD0
#f:0x0;0x801C865-0x20004470
#f:0x0;0x801C86D-0x20003D58
#f:0x0;0x802984F-0x20004360
#m:0x20004360;0x8029239-160
#m:0x20003CD0;0x801C9D9-64
#m:0x20004930;0x801C9F1-64
#m:0x20003D58;0x801CA17-16
#m:0x20004918;0x801F1FF-12
#m:0x200048D0;0x801E8B7-56
#m:0x20004880;0x801E24B-64
#m:0x20004470;0x802B937-1024
#f:0x0;0x801DD2B-0x20004880
#f:0x0;0x801E929-0x200048D0
#f:0x0;0x801F25D-0x20004918
#f:0x0;0x802B093-0x20004470
#f:0x0;0x801C85D-0x20003CD0
#f:0x0;0x801C865-0x20004930
#f:0x0;0x801C86D-0x20003D58
#f:0x0;0x802984F-0x20004360
#m:0x20003CF8;0x802ADBD-31
#m:0x20004370;0x8028B25-137
#m:0x20003CD0;0x8028B25-21
#f:0x0;0x802A449-0x20004370
#f:0x0;0x802A3AF-0x20003CF8
#m:0x20004360;0x8029239-160
#m:0x20004930;0x801C9D9-64
#m:0x200048E0;0x801C9F1-64
#m:0x20003D08;0x801CA17-16
#m:0x20003D60;0x801F1FF-12
#m:0x20004898;0x801E8B7-56
#m:0x20004848;0x801E24B-64
#f:0x0;0x801DD2B-0x20004848
#f:0x0;0x801E929-0x20004898
#f:0x0;0x801F25D-0x20003D60
#f:0x0;0x801C85D-0x20004930
#f:0x0;0x801C865-0x200048E0
#f:0x0;0x801C86D-0x20003D08
#f:0x0;0x802984F-0x20004360
#f:0x0;0x8028CAD-0x20003CD0

MemoryStats:
        Bytes allocated currently: 4643
        Max bytes allocated at a given time: 6103
        Cumulative sum of bytes ever allocated: 9868
        Current number of bytes allocated for the heap: 19432
        Current number of allocations: 14
        Number of failed allocations: 0 

#m:0x20003D08;0x801B2AB-12
#f:0x0;0x801B301-0x20003D08

Cumulative Stack Info:
        Maximum number of bytes used on the stack: 2232
        Current number of bytes allocated for the stack: 5888
        Number of stacks stats accumulated in the structure: 3 

#m:0x20004360;0x8029239-160
#m:0x20003CD0;0x801C9D9-64
#m:0x20004930;0x801C9F1-64
#m:0x20003D58;0x801CA17-16
#m:0x20004918;0x801F1FF-12
#m:0x200048D0;0x801E8B7-56
#m:0x20004880;0x801E24B-64
#m:0x20004470;0x802B937-1024
#f:0x0;0x801DD2B-0x20004880
#f:0x0;0x801E929-0x200048D0
#f:0x0;0x801F25D-0x20004918
#f:0x0;0x802B093-0x20004470
#m:0x20004918;0x801F1FF-12
#m:0x200048D0;0x801E8B7-56
#m:0x20004880;0x801E24B-64
#m:0x20004470;0x802B937-1024
#f:0x0;0x801DD2B-0x20004880
#f:0x0;0x801E929-0x200048D0
#f:0x0;0x801F25D-0x20004918
#f:0x0;0x802B093-0x20004470
#f:0x0;0x801C85D-0x20003CD0
#f:0x0;0x801C865-0x20004930
#f:0x0;0x801C86D-0x20003D58
#f:0x0;0x802984F-0x20004360

MemoryStats:
        Bytes allocated currently: 4643
        Max bytes allocated at a given time: 6103
        Cumulative sum of bytes ever allocated: 12496
        Current number of bytes allocated for the heap: 19432
        Current number of allocations: 14
        Number of failed allocations: 0 

#m:0x20003D08;0x801B2AB-12
#f:0x0;0x801B301-0x20003D08

Cumulative Stack Info:
        Maximum number of bytes used on the stack: 2232
        Current number of bytes allocated for the stack: 5888
        Number of stacks stats accumulated in the structure: 3 

#m:0x20004360;0x8029239-160
#m:0x20003CD0;0x801C9D9-64
#m:0x20004930;0x801C9F1-64
#m:0x20003D58;0x801CA17-16
#m:0x20004918;0x801F1FF-12
#m:0x200048D0;0x801E8B7-56
#m:0x20004880;0x801E24B-64
#m:0x20004470;0x802B937-1024
#f:0x0;0x801DD2B-0x20004880
#f:0x0;0x801E929-0x200048D0
#f:0x0;0x801F25D-0x20004918
#f:0x0;0x802B093-0x20004470
#m:0x20004918;0x801F1FF-12
#m:0x200048D0;0x801E8B7-56
#m:0x20004880;0x801E24B-64
#m:0x20004470;0x802B937-1024
#f:0x0;0x801DD2B-0x20004880
#f:0x0;0x801E929-0x200048D0
#f:0x0;0x801F25D-0x20004918
#f:0x0;0x802B093-0x20004470
#f:0x0;0x801C85D-0x20003CD0
#f:0x0;0x801C865-0x20004930
#f:0x0;0x801C86D-0x20003D58
#f:0x0;0x802984F-0x20004360
#m:0x20004360;0x8029239-160
#m:0x20003CD0;0x801C9D9-64
#m:0x20004930;0x801C9F1-64
#m:0x20003D58;0x801CA17-16
#m:0x20004918;0x801F1FF-12
#m:0x200048D0;0x801E8B7-56
#m:0x20004880;0x801E24B-64
#m:0x20004470;0x802B937-1024
#f:0x0;0x801DD2B-0x20004880
#f:0x0;0x801E929-0x200048D0
#f:0x0;0x801F25D-0x20004918
#f:0x0;0x802B093-0x20004470
#f:0x0;0x801C85D-0x20003CD0
#f:0x0;0x801C865-0x20004930
#f:0x0;0x801C86D-0x20003D58
#f:0x0;0x802984F-0x20004360
#m:0x20003CF8;0x802ADBD-31
#m:0x20004370;0x8028B25-137
#m:0x20003CD0;0x8028B25-21
#f:0x0;0x802A449-0x20004370
#f:0x0;0x802A3AF-0x20003CF8
#m:0x20004360;0x8029239-160
#m:0x20004930;0x801C9D9-64
#m:0x200048E0;0x801C9F1-64
#m:0x20003D08;0x801CA17-16
#m:0x20003D60;0x801F1FF-12
#m:0x20004898;0x801E8B7-56
#m:0x20004848;0x801E24B-64
#f:0x0;0x801DD2B-0x20004848
#f:0x0;0x801E929-0x20004898
#f:0x0;0x801F25D-0x20003D60
#f:0x0;0x801C85D-0x20004930
#f:0x0;0x801C865-0x200048E0
#f:0x0;0x801C86D-0x20003D08
#f:0x0;0x802984F-0x20004360
#f:0x0;0x8028CAD-0x20003CD0

MemoryStats:
        Bytes allocated currently: 4643
        Max bytes allocated at a given time: 6103
        Cumulative sum of bytes ever allocated: 17209
        Current number of bytes allocated for the heap: 19432
        Current number of allocations: 14
        Number of failed allocations: 0 

#m:0x20003D08;0x801B2AB-12
#f:0x0;0x801B301-0x20003D08

Cumulative Stack Info:
        Maximum number of bytes used on the stack: 2232
        Current number of bytes allocated for the stack: 5888
        Number of stacks stats accumulated in the structure: 3 
1 Like

To check heap and stack usage in an Mbed project using the ARM ToolChain:

  1. Enable the MBED_STACK_STATS_ENABLED and MBED_HEAP_STATS_ENABLED macros in your project.
  2. Compile your project.
  3. Check the heap and stack usage in the MBED OS 5 diagnostic output.