Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Official freeRam() command #73

Open
Testato opened this issue Aug 25, 2016 · 16 comments
Open

Official freeRam() command #73

Testato opened this issue Aug 25, 2016 · 16 comments

Comments

@Testato
Copy link

Testato commented Aug 25, 2016

All mcu project may pay attention on Ram use, and have an official command for print freeRam at runtime it is very important.
For avr there is a well know freeram function, but it do not work on other architecture, like Zero, Due, etc.

So i think that an official freeRam() function is actually very needed inside the Arduino core, and this also would push third-party core authors to work on their implementation of freeRam().

@vbextreme

This comment was marked as off-topic.

2 similar comments
@gpb01

This comment was marked as off-topic.

@max00xam

This comment was marked as off-topic.

@NicoHood
Copy link

hey guys we now have this +1 function on github, so why not use it instead of spamming? (might not be useable on phones i've heard)

@Testato

This comment was marked as off-topic.

@Victor795

This comment was marked as off-topic.

@PaulStoffregen

This comment was marked as off-topic.

@connornishijima

This comment was marked as off-topic.

@cmaglie
Copy link
Member

cmaglie commented Aug 26, 2016

Can someone post the function for AVR? Does anyone has the equivalent for the other architectures?

@Testato
Copy link
Author

Testato commented Aug 26, 2016

AVR

int freeRam () 
{
extern int __heap_start, *__brkval; 
int v; 
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

@matthijskooijman
Copy link
Collaborator

Note that the concept of "free RAM" might not be as straightforward as it seems, in particular in the face of dynamic memory allocation and fragmentation, which the above does not consider. This also gives a momentary measurement of the free RAM, with the current stack usage, which people might not understand intuitively. It might still be useful to have such a function, but documentation should be clear on its limitations.

@per1234
Copy link
Collaborator

per1234 commented Aug 26, 2016

Here's another version: https://github.com/McNeight/MemoryFree
I find it useful for detecting memory leaks.

@Testato
Copy link
Author

Testato commented Aug 26, 2016

@matthijskooijman
yes, this avr freeRam() version return only the free space beetwen the heap and the stack, but this is the most important value to check, because the other free space that can be inside the heap, caused by memory heap frammentation, collaborates to reduce the space between heap/stack.

So if we can add this second info also it is good, but not essential.
In this case we may return two value from freeRam() call

  • the ram value remaining between Heap and Stack
  • the fragmented free heap ram value

@vbextreme
Copy link

vbextreme commented Aug 26, 2016

I think the name is misleading function.
taking a cue from the posted code would be nice to something like:

unsigned int stackAvailable() 
{
    extern int __heap_start, *__brkval; 
    unsigned int v; 
    return (unsigned int)&v - (__brkval == 0 ? (unsigned int)&__heap_start : (unsigned int)__brkval); 
}

unsigned int heapAvailable()
{
    unsigned int total = stackAvailable();

    struct __freelist* current;
    extern struct __freelist *__flp;
    for (current = __flp; current; current = current->nx)
    {
        total += 2; /* Add two bytes for the memory block's header  */
        total += (unsigned int) current->sz;
    }

    return total;
}

@hanabanana

This comment was marked as off-topic.

@Pharap
Copy link

Pharap commented Aug 16, 2018

Being pedantic, none of these examples get the types correct.
According to the standard, ptrdiff_t should be used to store the result of subtracting pointers, so the function ought to be something like:

#include <stddef.h>

inline ptrdiff_t getAvailableStackSpace(void)
{
	extern int __heap_start;
	extern int * __brkval; 
	
	const int stackTop;
	
	return static_cast<ptrdiff_t>(&stackTop - ((__brkval == 0) ? &__heap_start : __brkval));
}

ptrdiff_t still has the disadvantage of being signed, but it's the correct type for an expression where one pointer is subtracted from another.

I don't know whether it would be semantically wise to try to cast ptrdiff_t, but if that's possible then it might be worth casting to size_t afterwards, as in:

#include <stddef.h>

inline size_t getAvailableStackSpace(void)
{
	extern int __heap_start;
	extern int * __brkval; 
	
	const int stackTop;
	
	return static_cast<size_t>(static_cast<ptrdiff_t>(&stackTop - ((__brkval == 0) ? &__heap_start : __brkval)));
}

@sandeepmistry sandeepmistry transferred this issue from arduino/Arduino Sep 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests