My Memory will lose every function runned! helpme

i want to use 3 function in this case, controlling ir remote, sending dht data, and controlling relay with mysql connector lib.
but if i run 2 function or 3 function after that my code is broke or cant run or nothing response.
i try to displaying free memory in serial monitor and yes, every my code is running, my free memory is lose.
how to fix it?
because i want to running this code for 24H non stop every day.
and this is my code

#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <Dns.h>
#include <MySQL_Cursor.h>
#include <IRremote.h>
#include <DHT.h>
#include <MemoryFree.h>
DHT dht(6, DHT11);
IRsend irsend;

uint16_t rawPower[68]={
  4510, 4498, 558, 1682, 554, 1682, 554, 1682, 
  558, 566, 554, 566, 554, 566, 550, 566, 
  554, 566, 554, 1682, 554, 1686, 554, 1682, 
  554, 566, 554, 566, 554, 566, 554, 566, 
  554, 566, 550, 566, 554, 1682, 558, 566, 
  554, 566, 550, 566, 554, 566, 554, 566, 
  554, 566, 554, 1682, 554, 566, 554, 1682, 
  554, 1682, 558, 1682, 554, 1682, 558, 1682, 
  554, 1682, 554, 1000};

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char hostname[] = "*******.com";
char user[] = "*******";
char password[] = "*******";
char query[] = "SELECT * FROM ******* where user='*******'";
char INSERT_SQL[] = "UPDATE *******.dynamic SET themp= '%d', humidity= '%d' WHERE user= '%s'";
char queryInsert[128];
char queryIR[] = "SELECT * FROM *******.IR where user='*******'";
char INSERT_IR_SQL[] = "UPDATE *******.IR SET value ='%s' WHERE user ='%s'";
char queryInsertIR[128];
EthernetClient client;
IPAddress server_ip;
MySQL_Connection conn((Client *)&client);
DNSClient dns_client;
void setup() { 
  Serial.begin(115200);  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  dht.begin();
  Ethernet.begin(mac_addr);
  // Begin DNS lookup
  dns_client.begin(Ethernet.dnsServerIP());
  dns_client.getHostByName(hostname, server_ip);
  Serial.println(server_ip);
  // End DNS lookup
  Serial.println("Connecting...");
  if (conn.connect(server_ip, 3306, user, password)) {
  Serial.println("CONNECTED!");
  }
  else
    Serial.println("Connection failed.");
}
void IR_void()
{
  MySQL_Cursor *cur_IR = new MySQL_Cursor(&conn);
  MySQL_Cursor *cur_insertIR = new MySQL_Cursor(&conn);
  sprintf(queryInsertIR, INSERT_IR_SQL, "NO","*******");
  cur_IR->execute(queryIR);
  column_names *coloums = cur_IR->get_columns();
  row_values *rowIR;
while(rowIR = cur_IR->get_next_row())
{
if (rowIR != NULL) {
        String rawValue=rowIR->values[3];
        String catIR=rowIR->values[2];
        if(rawValue=="POWER"){
  irsend.sendRaw(rawPower, 68, 38); //send raw data at 38KHz frequency
  cur_insertIR->execute(queryInsertIR);
  delete cur_insertIR;
break;
    }
}
}
delete cur_IR;
}

void loop() {
IR_void();
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query);
column_names *cols = cur_mem->get_columns();
row_values *row;
while(row = cur_mem->get_next_row()) {
    if (row != NULL) {
        String pin_value=row->values[5];
        String pin_number=row->values[2];
        const int number=pin_number.toInt();
        if(pin_value=="LOW")
        {
          digitalWrite(number, HIGH);
          }
          else if(pin_value=="HIGH")
          {
          digitalWrite(number, LOW);
            }
    }
    }
delete cur_mem;

int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
    sprintf(queryInsert, INSERT_SQL, temperature, humidity, "*******");
  MySQL_Cursor *cur_insert = new MySQL_Cursor(&conn);
  cur_insert->execute(queryInsert);
delete cur_insert;
    Serial.print("freeMemory()=");
    Serial.println(freeMemory());
}

Strings cause memory problems and program crashes on Arduino.

Instead, use zero terminated character arrays (C-strings) and the string functions to handle them.

        String rawValue=rowIR->values[3];
        String catIR=rowIR->values[2];

i think my problem not from that
i was change my string and delete that.
but my serial still get low memory

freeMemory()=6201
freeMemory()=6064
freeMemory()=5927
freeMemory()=5790
freeMemory()=5653
freeMemory()=5516

how to erase memory?
because if memory is loses every running, my code will hang when my memory is empty.

because if memory is loses every running, my code will hang when my memory is empty.

You appear to have no real clue what your program is doing, because your conclusion is complete nonsense.

You've been offered good advice, that you've chosen to ignore. Please explain where our motivation to offer more advice, pertaining to code you didn't post, is coming from.

Without actually running the code, I question why you are dynamically allocating memory for the SQL portion. While it appears that you are freeing the objects, I see no need for them to be dynamically created.

As to the other advice provided, please see if you can off load the String to string or c style strings.

i make it dynamically because my arduino need to update everytime for send dht data to databases, receive data value from db for controlling relay and switching everytime from website.
or you have other idea?
because i'am new in arduino and c program, so i just use what i see for make my way easier.
thanks for replay and i really appreciating for that :smiley:

i make it dynamically because my arduino need to update everytime for send dht data to databases,

This is not a valid reason to dynamically allocate memory.

so, what should i do for make a good code?

Use fixed character arrays, as large as reasonable, and make sure that you do not violate array bounds.

oke thanks, i will try it.

i want to use this.. this is good or not? : from complex select in example mysql connector lib.

  // Initiate the query class instance
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Supply the parameter for the query
  // Here we use the QUERY_POP as the format string and query as the
  // destination. This uses twice the memory so another option would be
  // to allocate one buffer for all formatted queries or allocate the
  // memory as needed (just make sure you allocate enough memory and
  // free it when you're done!).
  sprintf(query, QUERY_POP, 9000000);
  // Execute the query
  cur_mem->execute(query);
  // Fetch the columns and print them
  column_names *cols = cur_mem->get_columns();
  // Read the rows and print them
  row_values *row = NULL;
  do {
    row = cur_mem->get_next_row();
    if (row != NULL) {
      int a= 2;
        Serial.print(row->values[a]);
      Serial.println();
    }
  } while (row != NULL);
  // Deleting the cursor also frees up memory used
  delete cur_mem;

or i should use this? : from basic complex demonstration 2

  row_values *row = NULL;
  String head_count = "";

  delay(1000);
  Serial.println("2) Demonstrating using a local, global cursor.");
  // Execute the query
  cur.execute(query);
  // Fetch the columns (required) but we don't use them.
  cur.get_columns();
  // Read the row (we are only expecting the one)
  do {
    row = cur.get_next_row();
    if (row != NULL) {
      head_count = row->values[2];
    }
  } while (row != NULL);
  // Now we close the cursor to free any memory
  cur.close();

  // Show the result but this time do some math on it
  Serial.print("  NYC pop = ");
  Serial.println(head_count);