How to Free My RAM?

Hi @ All.

First i want to tell u what i need. then i tell u what my project does so u can find a better solution.

I want to Flush or Delete the RAM after every time my loop ends. ( or at least save this till the next stuff comes in ).
i dont know how to describe it better.

i set up a Costum page where my Arduino Mega Connect to with HTTP GET and he gets header Infos and a costum string " {0:0:0} " <- each number lights up an LED.

After X runs the LED all go out and the arduino stops. i dont want to manage my RAM better i want to clean all that not is from my Programm.

kind regarts Kiki

No need to do any thing like that. The stack is automatical freed at the end of a function call and the global data is just reused!.

Mark

Just open the gate and let him out. First ewe see him, then ewe don't :slight_smile:
Sorry just had to.

Edit: Sorry no offence was intended :frowning:

I searched the web for Hours, then i decided to post here to get this answers ...
As i said i need a way to clean my RAM..

holmes4:
No need to do any thing like that. The stack is automatical freed at the end of a function call and the global data is just reused!.

Mark

my RAM is full after X loops around 200-300
I did memoryFree to watch the mem and it drops from 6000free to 200 and then arduino just delivers 0:0:0 cause he cant process more data from server.

but what to say its the INTERNET where people make FUN of PEOPLE....

KikiTheOne:
I searched the web for Hours, then i decided to post here to get this answers ...

But you didn't think posting your code might be a good idea?

KikiTheOne:
I did memoryFree to watch the mem and it drops from 6000free to 200 and then arduino just delivers 0:0:0

You need to find the memory leak and plug it.

.ino

#include <SPI.h>
#include <Ethernet.h>
#include <String.h>


char myserver[] = "www.elohunter.net";
EthernetClient client;
String Input = "";
boolean StartCostum = true;
int LEDROT=53;
int LEDGRUEN=49;
int LEDBLAU=45;
//////////////////////

void setup(){
	pinMode(LEDROT, OUTPUT);
	digitalWrite(LEDROT, LOW);
	
	pinMode(LEDGRUEN, OUTPUT);
	digitalWrite(LEDGRUEN, LOW);
	
	pinMode(LEDBLAU, OUTPUT);
	digitalWrite(LEDBLAU, LOW);

	Ethernet.begin(mac);
	Serial.begin(9600); 
	Serial.println("Connecter Test");
}

void loop(){
  StartCostum = true;
  sendGET(); // call sendGET function below when byte is an e
  Serial.println(freeRam ());
} 

//////////////////////////
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

void sendGET() //client function to send/receive GET request data.
{
	if(StartCostum==true)
	{
		if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
			delay(250);
			client.println("GET /projects/1/index.php HTTP/1.0"); //download text
			client.println("Host: www.elohunter.net");
			client.println("User-Agent: ARD1-MEGA-ELOHUNTER");
			client.println(); //end of get request
		} 
		else {
			Serial.println("connection failed"); //error message if no client connect
			Serial.println();
		}

		while(client.connected() && !client.available()) delay(50); //waits for data
		while (client.connected() || client.available()) { 
			char d = client.read();
			String e = String(d);
			Input += e;
		}
		
		int firstClosingBracket = Input.indexOf('{');
		int lastClosingBracket = Input.indexOf('}');
		
		String Output_Coding = Input.substring(firstClosingBracket+1,lastClosingBracket);
		
		String Rval = getValue(Output_Coding, ':', 0);
		String Gval = getValue(Output_Coding, ':', 1);
		String Bval = getValue(Output_Coding, ':', 2);
		
		if(Rval.toInt()==1){digitalWrite(LEDROT, HIGH);}else{digitalWrite(LEDROT, LOW);}
		if(Gval.toInt()==1){digitalWrite(LEDGRUEN, HIGH);}else{digitalWrite(LEDGRUEN, LOW);}
		if(Bval.toInt()==1){digitalWrite(LEDBLAU, HIGH);}else{digitalWrite(LEDBLAU, LOW);}

		client.stop(); //stop client
		Input = "";
		StartCostum = false;
	}
}

String getValue(String data, char separator, int index)
{
	int found = 0;
	int strIndex[] = { 0, -1  };
	int maxIndex = data.length()-1;
	for(int i=0; i<=maxIndex && found<=index; i++)
	{
		if(data.charAt(i)==separator || i==maxIndex)
		{
			found++;
			strIndex[0] = strIndex[1]+1;
			strIndex[1] = (i == maxIndex) ? i+1 : i;
		}
	}
	return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

.php

if($_SERVER['HTTP_USER_AGENT']=='ARD1-MEGA-ELOHUNTER')
{
	include(".sql.php");

	print '{';

	$get_color = mysql_query("SELECT * FROM `project1_color_settings`");
	$count = 0;
	while($row = mysql_fetch_assoc($get_color))
	{
		if($count!=0){print ':';}
		print $row['value'];
		$count = $count + 1;
	}
	
	print '}';
}

The Problem is not the Server or the MYSQL in the PHP.
Freemem gives me like i said 6000~ back and then 2xx and then it stops the GET from server, but its still connected

Why is String Input = "" a global variable?

You should define this within sendGET Then the valuable memory it is wasting would be recovered as the sendGet function completes.

Three things.

  1. Never use String on the Arduinos. Aways use string.

  2. You have very little SRAM and a HTTP response will use it all up very quickly.

  3. String e = String(d);
    This is not the way to add a char to a string and is almost certainly the cause of your memory leak.

Mark

KenF:
Why is String Input = "" a global variable?

You should define this within sendGET Then the valuable memory it is wasting would be recovered as the sendGet function completes.

done
thanks for that

holmes4:
Three things.

  1. Never use String on the Arduinos. Aways use string.

  2. You have very little SRAM and a HTTP response will use it all up very quickly.

  3. String e = String(d);
    This is not the way to add a char to a string and is almost certainly the cause of your memory leak.

Mark

#1 what alternative could i use. i have to explode the string i get and the only way i found was this, if u have any ideas let me know :slight_smile:

#2 it can handle the response the header is not that big.

HTTP/1.1 200 OK
Date: Mon, 10 Nov 2014 12:58:19 GMT
Server: Apache/2.2.29 (Unix)
X-Powered-By: PHP/5.3.29
Connection: close
Content-Type: text/html
{1:1:1}

thats why i asked for the flush of RAM but now i already did what KenF said and i will test if that was the trick

#3 "String e = String(d);"
i have to convert the "d" to a String otherwhise it says cant ADD String on CHAR

thx alot so far. i will test this fixxes now and respond shortly

i have to convert the "d" to a String otherwhise it says cant ADD String on CHAR

Of course you can add a char to a String.

what alternative could i use. i have to explode the string i get and the only way i found was this, if u have any ideas let me know

There is in C/C++ a huge difference between String and string.!!!

Mark

I ran it for nearly 1 hour now and it looks like its still working fine. :slight_smile: i guess the

KenF:
Why is String Input = "" a global variable?

You should define this within sendGET Then the valuable memory it is wasting would be recovered as the sendGet function completes.

done the trick.
i keep watching thx so far :smiley:

Well thanks for the feedback. It's surprising how many people just get the answer then never return.

KenF:
Just open the gate and let him out. First ewe see him, then ewe don't :slight_smile:
Sorry just had to.

Edit: Sorry no offence was intended :frowning:

Ewe got there before me. But I had only thought of the gate.

...R