All
In my home automation system at http://www.2wg.co.nz I am intending to use caching to optimise system performance by storing emails and activity records for a short time in an in-memory cache which is processed (emails sent, activity log records written to a file on an SD card) when the system becomes inactive. Sending emails is typically a multi-second internet activity and the current process of writing activity records line by line (open file, append one activity record line, close the file) is also inefficient.
My in-memory cache will be a linked list using dynamic memory allocation. In order for the cache to work reliably I need to ensure that I do not add more records to the linked list cache than there is available memory. This requires my application to have a precise and current knowledge of current memory utilisation and to force cache processing (clearance by sending emails and writing out the activity records) whenever free memory is getting low - even if the application is very busy doing something and the cache processing operation would cause application delays.
I have now completed a reimplementation of my memory utilisation procedure and built linked list cache functionality in a prototype application. This application generates random email and activity records, appends then dynamically to the linked list cache and clears the cache at random intervals by pretending to send emails and write activity records to SD card log files. When I run the application through a loop 99 times I get a very good profile of what is going on with my application's memory through all 99 iterations.
For anyone who wants to analyse and manage Arduino application memory use and for those interested in using linked lists within their applications I will attach the prototype application source code to this thread in another post(s) to overcome the 9500 byte post limitation.
For people who don't like using Arduino Strings and claim they can fragment HEAP memory this program provides an excellent demonstration. However I would point out that a simple change to this application to fully clear the cache from time to time (send the emails AND write the log records at the same time - one after the other) would eliminate HEAP fragmentation and reduce HEAP size and FREE HEAP space to zero. (And this is what I will do in my production system.)
In my production home automation system I intend to implement the new memory utilisation analysis procedure and the linked list cache. I will also let the application manage free space and under various conditions (including when free ram drops below 750 bytes) fully process and clear the cache.
I run my applications on a Freetronics Ethermega card if you need to know that.
Here is a sample or the Serial monitor output for the prototype application:
Setup
DATA/BSS 1043, HEAP 0, FREE 7063, STACK 86, TOTAL 8192, FREE HEAP 0, FREE LIST <<NULL>>
DATA/BSS 1043, HEAP 67, FREE 6996, STACK 86, TOTAL 8192, FREE HEAP 39, FREE LIST 39
DATA/BSS 1043, HEAP 127, FREE 6936, STACK 86, TOTAL 8192, FREE HEAP 67, FREE LIST 40, 23, 4
DATA/BSS 1043, HEAP 212, FREE 6851, STACK 86, TOTAL 8192, FREE HEAP 36, FREE LIST 23, 9, 4
<<EMAILS SEND>> 1
DATA/BSS 1043, HEAP 127, FREE 6936, STACK 86, TOTAL 8192, FREE HEAP 67, FREE LIST 40, 23, 4
...
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 622, FREE LIST 290, 184, 61, 20, 20, 19, 9, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 594, FREE LIST 290, 184, 45, 20, 20, 9, 7, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 562, FREE LIST 290, 184, 20, 20, 13, 9, 7, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 444, FREE LIST 290, 99, 20, 9, 7, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 412, FREE LIST 290, 47, 20, 20, 9, 7, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 384, FREE LIST 290, 20, 20, 19, 9, 7, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 352, FREE LIST 270, 20, 20, 9, 7, 7, 7, 7, 5
DATA/BSS 1043, HEAP 1136, FREE 5927, STACK 86, TOTAL 8192, FREE HEAP 320, FREE LIST 238, 20, 20, 9, 7, 7, 7, 7, 5
...
DATA/BSS 1043, HEAP 458, FREE 6605, STACK 86, TOTAL 8192, FREE HEAP 254, FREE LIST 195, 20, 20, 8, 7, 4
DATA/BSS 1043, HEAP 458, FREE 6605, STACK 86, TOTAL 8192, FREE HEAP 222, FREE LIST 163, 20, 20, 8, 7, 4
DATA/BSS 1043, HEAP 458, FREE 6605, STACK 86, TOTAL 8192, FREE HEAP 190, FREE LIST 131, 20, 20, 8, 7, 4
<<FINAL CACHE TO BE CLEARED>>
<<LIST OF CACHE RECORDS>>
Type 3, Item Activity 2512
Type 0, Item Email Connection
Type 1, Item Email Line 1
Type 1, Item Email Line 2
Type 2, Item Email Disconnect
Type 3, Item Activity 8276
Type 4, Item HTML Request 5391
Type 5, Item Hack Attempt 2411
Type 5, Item Hack Attempt 8571
Cache Record Count 9
DATA/BSS 1043, HEAP 458, FREE 6605, STACK 86, TOTAL 8192, FREE HEAP 190, FREE LIST 131, 20, 20, 8, 7, 4
<<EMAILS SEND>> 1
<<LIST OF CACHE RECORDS>>
Type 3, Item Activity 2512
Type 3, Item Activity 8276
Type 4, Item HTML Request 5391
Type 5, Item Hack Attempt 2411
Type 5, Item Hack Attempt 8571
Cache Record Count 5
DATA/BSS 1043, HEAP 458, FREE 6605, STACK 86, TOTAL 8192, FREE HEAP 306, FREE LIST 147, 131, 20, 8
<<LOG FILE WRITE>> 5
<<LIST OF CACHE RECORDS>>
Cache Record Count 0
DATA/BSS 1043, HEAP 0, FREE 7063, STACK 86, TOTAL 8192, FREE HEAP 0, FREE LIST <<NULL>>
End
Happy to answer questions.
Cheers
Catweazle NZ