if you are not using 1.0.5 of the Arduino IDE, there is a serious bug in the library that means you run out of memory more often.
Just for a creditability check, weren't the memory loss issues corrected in version 1.0.4?
It looks like there were two fixes to malloc. The fix in 1.0.4 from Paul Stoffregen fixed the serious bug, but 1.0.5 had a merge from avr-libc 18.0 to fix another malloc problem. I stopped when I was looking through the ChangeLog at the first malloc fix, but the 1.0.4 fix was the one that everybody hit with the String class.
I think to think that the Arduino team should never have included the String class in the Arduino library, since dynamic allocation really does not make sense in a small memory environment.
In addition, the program segment does not work if second character to be read is not immediately available after the first character has been read, since the string will be reset immediately once the first loop that reads the characters finishes (i.e. no character available), and then it goes to test whether "on" or "off" appears somewhere in the string.
ARDUINO 1.0.5 - 2013.05.15
- [avr] malloc bug: backported avr-libc 1.8.0 implementation
- [avr] removed deprecated interrupt handlers causing compiler issues
with newer avr-gcc.
- [avr] added c_str() method to String
- [avr] Stream "_timeout" field and related methods are now protected
- Upgrades to WiFi library
- Fixed a bunch of examples
- Added Arduino Robot libraries
- Added TFT display library
- Upgrades to WiFi firmwares
- Backport from 1.5: install Library from .zip file or folder
- Updated windows drivers
- Added Windows installer
ARDUINO 1.0.4 - 2013.03.11
- Fixed malloc bug (Paul Stoffregen)
- Fixed memory leak when calling Ethernet.begin() multiple times.
- Fixed SD example listfiles.ino
- Fixed a lot of Esplora examples
- Added GSM library
- Sort entries in preferences.txt (Shigeru Kanemoto)
- Fixed some wrong translations
- Fixed NPE due to permissions IO error
- Updated drivers for Windows (all-in-one, signature for Win8)