Go Down

Topic: Memory Leaks (SD and Strings) (Read 5671 times) previous topic - next topic

mrjonny2

Jul 27, 2012, 05:05 pm Last Edit: Jul 31, 2012, 11:53 am by mrjonny2 Reason: 1
Hi all,

I'm playing around with writing strings to an SD card (that is triggered when a server connection is unavailable), it subsequently logs the sensor values to the SD card (within the regular loop() function) and once a connection to the server becomes available it sends this as a packet over UDP.

I seem to be running into some pretty large memory leaks that occur once this SD card write section is entered, which eventually causes the Arduino to run out of free memory.

Any help would be greatly appreciated,

Thanks

Jonny

el_supremo

Change your code so that it does not use the String library at all. There is a bug in the library.

Pete
Don't send me technical questions via Private Message.

MarkT

The problem is more fundamental - string libraries fundamentally need garbage collection, there isn't the resources to do this on a small microcontroller, you have to manage memory use at a low level yourself.  The String library shouldn't be part of the distrubution, its stupid to have it and only gives people grief I think.
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

mrjonny2

Is there any function or library that doesn't leak memory like a bullet ridden bucket that I can swap it out for short for rewriting sections of code using arrays?
Thanks for the heads up guys.

wanderson


The problem is more fundamental - string libraries fundamentally need garbage collection, there isn't the resources to do this on a small microcontroller, you have to manage memory use at a low level yourself.  The String library shouldn't be part of the distrubution, its stupid to have it and only gives people grief I think.


It is not simply a lack of garbage collection, the String library has real memory leaks, which have been documented.  At least one person (Paul Stoffhegen) has fixed the problem and submitted those fixes to the Team.  Apparently they remove most of the fixes when they incorporated his submission... No comments as to why.  The end result is that using Strings in the Arduino is not a good idea.

I am aware of no memory leaks with the SD library, but it does use a lot of SRAM...


Docedison

I thing that both the posters that answered you were saying no... in different manners. I agree with the garbage collection 'defect' I cannot see where it would be easily possible with the limited ram resources that the 328 or the 2560  have available. It is my impression that the String Functions are there simply because the Prime purpose of the Arduino is education, that there is enough of a compiler and enough of the complete C and C++ language present to do real work besides n universal greeting... Hello...  is very commendable as it gives a good picture of the language... BUT there are numerous warnings about not using Strings spread throughout this forum, it's participants and both the reference and playground area's and a complete dearth of ANY string examples (and those have warnings too)... For example I have a use for Strings and a dictionary so I can compose Strings as messages W/o having to use the actual strings until print time... But when I get there I will be using strings, arrays of type char. I have both the Renasas and STmicro32 explorer development kits as well as that motorola MP430 device (They were cheap) and one of those will be the final destination of my final code... once I learn to write code. I can answer any electronics question you might have... Or tell you that I just don't know. I do know that when I wrote my first variant on "Hello World" using a Bosch BMP085 I needed a Lot of help. Both with the architecture and the code to make the device work.
AS an older curmudgeon I can tell you that the warnings are there for a reason. I just had a thought... There is a device called a Rampack that will give you several hundred KB of additional ram and perhaps with the additional Ram Space it might for example to designate an area for your Strings where there is enough resources to code around the inherent small device limitations. When I started writing code to go with my electronics designs the biggest Processor I could find was a PIC 16C57... 2K of program space... broken into 2 1K pages... and I wrote the code in a variant of a '51 assembler dialect called Spasm... Never Again.

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

mrjonny2

Hey guys thanks for all your assistance.

As it turned out it was the String library. Having removed all traces of it from my code and replacing them with arrays of characters instead I'm now not seeing any memory leaks.

Go Up