CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How do you find remaining free memory?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
smanzer



Joined: 14 Sep 2009
Posts: 24
Location: Ontario, Canada

View user's profile Send private message Visit poster's website

How do you find remaining free memory?
PostPosted: Fri Jun 11, 2010 8:19 am     Reply with quote

Good Morning,

Can someone tell me how to find out how much free ram memory is left on the PIC?

I use a lot of dynamic linked lists and it would be nice to be able to find out how much is left prior to starting a routine.

The lists use malloc and free which allocate and free memory. I do not know how to find out how much unused memory is left though.

Thanks in advance,

Steven
_________________
"I am always doing that which I can not do, in order that I may learn how to do it."
- Pablo Picasso
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri Jun 11, 2010 8:29 am     Reply with quote

After compiling, open the .lst file (in the IDE, 'C/ASM List').
At the top, it show you the ROM used, and the RAM used. The 'worst case' figure. Subtract this from the total RAM available.

Best Wishes
smanzer



Joined: 14 Sep 2009
Posts: 24
Location: Ontario, Canada

View user's profile Send private message Visit poster's website

PostPosted: Fri Jun 11, 2010 8:52 am     Reply with quote

Thank you Ttelmah,

Is there any way to find out during program execution how much ram is still available?

Steven
_________________
"I am always doing that which I can not do, in order that I may learn how to do it."
- Pablo Picasso
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri Jun 11, 2010 9:01 am     Reply with quote

It won't change.
There is no 'dynamic' memory allocation, unless you are using a library like STDIBM.
If so, the variables holding the memory size, and where the nodes currently point, are available to you, to tell you what space is still available.

Best Wishes
smanzer



Joined: 14 Sep 2009
Posts: 24
Location: Ontario, Canada

View user's profile Send private message Visit poster's website

PostPosted: Sun Jun 13, 2010 7:13 am     Reply with quote

Hi Ttelmah,

You are right, malloc uses <stdlibm.h>

I must not know how malloc works. It was my understanding that malloc (from Wikipedia: In computing, malloc is a subroutine for performing dynamic memory allocation in the C and C++ programming languages, though its use in C++ has been largely superseded by operators new and new[]. malloc is part of the standard library for both languages and is declared in the stdlib.h header (for C++, it is also declared within the std namespace via the cstdlib header).)

I use many linked lists (http://en.wikipedia.org/wiki/Linked_list) and the individual data structure is defined once (which the compiler can see), but the memory locations for the actual data is dynamically allocated and released as needed.

I have looked through the getenv() documentation and do not see anything for RAM. There are entries for the following:
getenv("PROGRAM_MEMORY") getenv("DATA_EEPROM"));

Is it possible that an eerom is used with malloc? The ccs help states: "The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate." It does not specify they type of memory that is allocated.

Does anyone know of a way to figure out how much RAM memory is available during the execution of the program? Obviously I will know where the individual nodes reside in memory (from malloc), but I have no idea how that would tell me how much space is available. Do you use the pointer to the node and compare it to the end of RAM location? If that is how it is done, what is the variable that is used to find the end of RAM?

I am hoping someone has come across a similar need and has figured out a work around....

Thanks,

Steven
_________________
"I am always doing that which I can not do, in order that I may learn how to do it."
- Pablo Picasso
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 13, 2010 1:45 pm     Reply with quote

Here's a little test program that I made to experiment with the problem.
Initially, I think the heap_size() macro that I wrote would tell you the size
of available ram. But if you use malloc(), and then you free some
blocks so it becomes segmented, then the simple method shown below
would not work.
Code:

#include <18F452.h>
#USE DYNAMIC_MEMORY
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//int8 array[1516];

#define initial_heap_size() (__DYNAMIC_HEAD ? (getenv("RAM") - __DYNAMIC_HEAD) : 0)

//======================================
void main(void)
{
int16 temp;

temp = initial_heap_size();

//printf("Dynamic Head: %lu \r", __DYNAMIC_HEAD);

printf("Free RAM: %lu \r", temp);

while(1);
}

I don't really want to work on it much more than this. This was just an idea.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group