![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
out of RAM,,,,,,any advice?? |
Posted: Wed Jul 06, 2005 4:00 am |
|
|
Hi,
I am working on a big project that has a LCD interface as well as a DSP interface. Though I am using 18f8722 which comes with huge in-chip RAM, I am running out of it because of the complication and the huge size of the code.... ( I look at the list file and on top of that it says RAM USED: 89%!!!! )
Can anybody give some advice on how to save some RAM and optimise the code?
Will it work by transferring local variables to global ones or by putting some variables into bank1,2,3 ????
I am just guessing, ,,,,,please give some deatiled methods...
Many thanks |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Jul 06, 2005 4:22 am |
|
|
Manual placement, may sometimes help when trying to put large arrays into memory, but in general doesn't help.
The use of global or local variables, will depend on what you are doing. If (for instance), you have two routines that follow one another in your program sequence, and both use global variables without needing to (the values do not need to be maintained between subsequent calls - achievable by either global, or static variables, and the values do not need to pass to another routine), then using global variables, will _waste_ RAM. If local variables are used, the compiler can re-use this storage in the second routine, but if global variables are used, it is permanently reserved, and will make things worse...
Start by carefully listing what variables do need to be global. Follow this by those that need to be 'static'. Make sure that only variables that need to be global or static, are defined as such. Then see if you can split up functions. If (for instance), you have a single function, that performs three seperate blocks of operations, that do not connect to one another, ad do not need to hand variables to each other, split these into seperate functions, and use local variables in these. If for example, you had code that took a string, printed this, then did some arithmetic on an incoming number, then did some keyboard I/O, storage will be being used by all of these seperately. If the printing was put into one subroutine, then the arithmetic into another, and then the keyboard I/O into a third, the storage areas of each part, can use the same memory areas!...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
|
Posted: Wed Jul 06, 2005 5:02 am |
|
|
Hi, Ttelmah,
First of all, thanks for your detailed explanations!
During the whole development time, I have been modularizing the part of the code so that individual functions don't carry out more than one task but never didn't know this would help save the memory space!! I did it on one function just now for testing and it worked!! But how come when you split up a funciton into pieces they would be sharing the same space? I can't figure out why really!! and Will this increase overhead of function callings?
The list file gives the general RAM usage, i am wondering where i can gain an idea of memory allocation, ie, how much of bank 0(the default bank) has been taken up and how much spare space are there in bank 2,3,4..........................do you think it would help by looking at this bit?
finally, can i end up with the conclusion that using global variables would cost more memory than using local ones? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Jul 06, 2005 5:28 am |
|
|
There is a 'pool' of memory.
Now if you have a function, that then calls another _inside itself_. then the second function cannot use any part of this pool that is 'in use' by the first, since the first function is still 'live', and expects things to be as they were when the second function returns. However if the two functions occur one after the other, then the second function can use the areas that have been used, and 'finished with' by the first function. Marking a variable as 'static', flags to the compiler, that this variable is never 'finished with', ad cannot be re-used. Marking it as 'global', has a similar effect.
So:
Code: |
//Any memory area used for variables here cannot be re-used.
void sub_1() {
//Any memory area declared for variables here, can be re-used
//_once_ sub_1 has finished, provided it is not 'static'.
}
void sub_2() {
//This can use the same memory area as sub_1
}
void sub_3() {
//This also can use the areas used in sub_1 or sub_2, but _not_
//in sub_4, because this is caled from sub_4
}
void sub_4() {
//This can use the areas used in sub_1, and sub_2, but any areas
//used hre, cannot be used in sub_3
sub_3();
}
main () {
//Any memory declared for variables here, cannot be re-used
sub_1();
sub_2();
sub_4();
}
|
The key is to realise that normal variables are strictly 'temporary' storage, and they have a sequence 'life', as well as a physical location. Once sub_1, has completed, any temporary storage locations used (all normally declared variables), are once again available for the compiler to re-allocate.
This is why you have to be careful to declare counters tha you want to 'survive' between subsequent calls, as 'static', or as global variables. However the downside of these declarations is that the space cannot then be re-used... :-(
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
|
Posted: Wed Jul 06, 2005 5:39 am |
|
|
Ahha, I got it! This is brilliant. Many thanks mate
What I will do is, I would modularize all the possible functions and carefully declare the global and local variables to see what benefits I would end up with( gosh, this would be long!! The code has thousands of lines!! )
I may pop up again if further compression/optimisation is needed
Thanks again mate |
|
![](templates/subSilver/images/spacer.gif) |
piedos
Joined: 14 Mar 2005 Posts: 5 Location: TURKEY/Ankara
|
thanks for perfect explanation |
Posted: Wed Aug 10, 2005 3:16 am |
|
|
the sample code is really perfect for beginners
thank you Ttelmah _________________ ---------------------------------------------------------
The only thing that equally given mankind is time. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|