Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4972

General • Re: Free Memory (using C SDK).

$
0
0
Hi all,

Is there a way of getting the free memory at runtime for a program using the C SDK? I'm not currently using dynamic allocation e.g. malloc, so could probably estimate, but it would be good if there was a function that could give me a run time reading.

Cheers.

Phill.
Yes you can get the free memory however it will not tell you how large the biggest continues block is. If memory is fragmented then you will have trouble getting a large block.

Make sure you have this header included

Code:

#include<malloc.h>ORuint32_t getTotalHeap(void);uint32_t getFreeHeap(void);
This will give you an Approximate of the free memory

Code:

printf ("System Reports Approximately %d bytes free\n\r", getFreeHeap());
If you want to see the largest block you can use a trick I used

Code:

int ptsh_mem (int argc, char** argv){    char* free_memory = NULL;    uint8_t x;    for (x = 1; x < 250; x++)    {        free_memory = malloc(1024 * x);        if (free_memory == NULL)        {            break;        }        free(free_memory);        free_memory = NULL;    }    printf ("System Reports Approximately %d K free\n\r", (x - 1));    return 0;}
To do this you must set this in you CMakeLists.txt

target_compile_definitions({PROJECT NAME} PUBLIC -DPICO_MALLOC_PANIC=0)

How are you thinking of using this memory? if you re going to be freeing it back and reallocating it I would not use the SDK memory manager. If you need details just ask.

Statistics: Posted by DarkElvenAngel — Sat Nov 02, 2024 2:10 pm



Viewing all articles
Browse latest Browse all 4972

Trending Articles