SD.h library are making unusual file

i'm using SD.h library for saving many parameter like SSID, tuning variable, historical data(only a number), etc on my wemos d1 mini pro (esp8266).

but suddenly my esp was randomly rewriting my file data by it self (like SSID file, or some of historical data), the problem is, the file that my esp write are 4Gb in size. because of that my ESP are not running to void loop. it's restarting on it's own because it can't read the file.

anyone know what cause the ESP rewriting the file?

edit

here's the data load and save code

void checkdosis() {

  File dosfile;
  if (!SD.begin(4)) {
    //    int sdtrying = 0;
    Serial.println("Initializing Failed");
    dataSeparator();
    //    sdtrying++;
    return;
  }

  if (SD.exists("dosage.txt")) {
    Serial.println("dosage file exist");
    //    dataSeparator();
  } else {
    Serial.println("dosage file doesn't exist");
    Serial.println("creating File");
    delay(1000);
    dosfile = SD.open("dosage.txt", FILE_WRITE);
    dosfile.print("3");
    dosfile.close();
    //    dataSeparator();
  }


  DosisFile = SD.open("dosage.txt");
  if (DosisFile) {

    while (DosisFile.available()) {
      char ltrDosis = DosisFile.read();
      strDosis += ltrDosis;
    }

    Dosis = strDosis.toInt();
    DosisFile.close();
  } else {
    Serial.print("error Opening dosage");
  }

}

void checkmultiplier() {

  if (!SD.begin(4)) {
    //    int sdtrying = 0;
    Serial.println("Initializing Failed");
    dataSeparator();
    //    sdtrying++;
    return;
  }

  if (SD.exists("multiplier.txt")) {
    Serial.println("multiplier file exist");
    //    dataSeparator();
  } else {
    Serial.println("multiplier file doesn't exist");
    Serial.println("creating File");
    delay(1000);
    muliplierFile = SD.open("multiplier.txt", FILE_WRITE);
    muliplierFile.print("100");
    muliplierFile.close();
    //    dataSeparator();
  }

  muliplierFile = SD.open("multiplier.txt");
  if (muliplierFile) {
    while (muliplierFile.available()) {
      char ltrmultip = muliplierFile.read();
      strMultip += ltrmultip;
    }
    multiplier = strMultip.toInt();
    muliplierFile.close();
  } else {
    Serial.print("error Opening multiplier");
  }

}

void checkhistori() {

  if (!SD.begin(4)) {
    //    int sdtrying = 0;
    Serial.println("Initializing Failed");
    dataSeparator();
    //    sdtrying++;
    return;
  }

  if (SD.exists("totalRunT.txt")) {
    Serial.println("totalRunT file exist");
    //    dataSeparator();
  } else {
    Serial.println("history file doesn't exist");
    Serial.println("creating File");
    delay(1000);
    RunT = SD.open("totalRunT.txt", FILE_WRITE);
    RunT.print("0");
    RunT.close();
    //    dataSeparator();
  }

  RunT = SD.open("totalRunT.txt");
  if (RunT) {
    while (RunT.available()) {
      char ltrRunT = RunT.read();
      strRunT += ltrRunT;
    }
    RunTime = strRunT.toInt();
    RunT.close();
  } else {
    Serial.print("error Opening RunTime");
  }

}

void checkresidue() {

  if (!SD.begin(4)) {
    //    int sdtrying = 0;
    Serial.println("Initializing Failed");
    dataSeparator();
    //    sdtrying++;
    return;
  }

  if (SD.exists("lastResidue.txt")) {
    Serial.println("lastResidue file exist");
    //    dataSeparator();
  } else {
    Serial.println("lastResidue file doesn't exist");
    Serial.println("creating File");
    delay(1000);
    ResidueFile = SD.open("lastResidue.txt", FILE_WRITE);
    ResidueFile.print("0");
    ResidueFile.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  ResidueFile = SD.open("lastResidue.txt");
  if (ResidueFile) {
    while (ResidueFile.available()) {
      char ltrResidue = ResidueFile.read();
      strHresidue += ltrResidue;
    }
    Residu = strHresidue.toInt();
    ResidueFile.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening lastResidue");
  }

}

void checkTune() {
  String strTune1;
  String strTune2;
  String strTune3;
  String strTune4;
  String strTune5;

  if (!SD.begin(4)) {
    //    int sdtrying = 0;
    Serial.println("Initializing Failed");
    dataSeparator();
    //    sdtrying++;
    return;
  }

  if (SD.exists("tune1.txt")) {
    Serial.println("tune1 file exist");
    //    dataSeparator();
  } else {
    Serial.println("tune1 file doesn't exist");
    Serial.println("creating File");
    delay(100);
    tune1file = SD.open("tune1.txt", FILE_WRITE);
    tune1file.print("1000");
    tune1file.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  tune1file = SD.open("tune1.txt");
  if (tune1file) {
    while (tune1file.available()) {
      char ltrTune1 = tune1file.read();
      strTune1 += ltrTune1;
    }
    inttune1 = strTune1.toInt();
    tune1 = inttune1;
    tune1file.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening tune1");
  }

  //////////   TUNE 2   //////////

  delay(50);
  if (SD.exists("tune2.txt")) {
    Serial.println("tune2 file exist");
    //    dataSeparator();
  } else {
    Serial.println("tune2 file doesn't exist");
    Serial.println("creating File");
    delay(100);
    tune2file = SD.open("tune2.txt", FILE_WRITE);
    tune2file.print("1000");
    tune2file.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  tune2file = SD.open("tune2.txt");
  if (tune2file) {
    while (tune2file.available()) {
      char ltrTune2 = tune2file.read();
      strTune2 += ltrTune2;
    }
    inttune2 = strTune2.toInt();
    tune2 = inttune2;
    tune2file.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening tune2");
  }

  //////////   TUNE 3   //////////

  delay(50);
  if (SD.exists("tune3.txt")) {
    Serial.println("tune3 file exist");
    //    dataSeparator();
  } else {
    Serial.println("tune3 file doesn't exist");
    Serial.println("creating File");
    delay(100);
    tune3file = SD.open("tune3.txt", FILE_WRITE);
    tune3file.print("1000");
    tune3file.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  tune3file = SD.open("tune3.txt");
  if (tune3file) {
    while (tune3file.available()) {
      char ltrTune3 = tune3file.read();
      strTune3 += ltrTune3;
    }
    inttune3 = strTune3.toInt();
    tune3 = inttune3;
    tune3file.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening tune3");
  }

  //////////   TUNE 4   //////////

  delay(50);
  if (SD.exists("tune4.txt")) {
    Serial.println("tune4 file exist");
    //    dataSeparator();
  } else {
    Serial.println("tune4 file doesn't exist");
    Serial.println("creating File");
    delay(100);
    tune4file = SD.open("tune4.txt", FILE_WRITE);
    tune4file.print("1000");
    tune4file.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  tune4file = SD.open("tune4.txt");
  if (tune4file) {
    while (tune4file.available()) {
      char ltrTune4 = tune4file.read();
      strTune4 += ltrTune4;
    }
    inttune4 = strTune4.toInt();
    tune4 = inttune4;
    tune4file.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening tune4");
  }

  //////////   TUNE 5   //////////

  delay(50);
  if (SD.exists("tune5.txt")) {
    Serial.println("tune5 file exist");
    //    dataSeparator();
  } else {
    Serial.println("tune5 file doesn't exist");
    Serial.println("creating File");
    delay(100);
    tune5file = SD.open("tune5.txt", FILE_WRITE);
    tune5file.print("1000");
    tune5file.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  tune5file = SD.open("tune5.txt");
  if (tune5file) {
    while (tune5file.available()) {
      char ltrTune5 = tune5file.read();
      strTune5 += ltrTune5;
    }
    inttune5 = strTune5.toInt();
    tune5 = inttune5;
    tune5file.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening tune5");
  }
}

void RunTSave() {

  SD.remove("totalRunT.txt");

  RunT = SD.open("totalRunT.txt", FILE_WRITE);
  RunT.print(RunTime);
  RunT.close();

}

void ResidueSave() {

  SD.remove("lastResidue.txt");

  ResidueFile = SD.open("lastResidue.txt", FILE_WRITE);
  ResidueFile.print(Residu);
  ResidueFile.close();

}

void saveDos() {

  SD.remove("dosage.txt");

  DosisFile = SD.open("dosage.txt", FILE_WRITE);
  DosisFile.print(Dosis);
  DosisFile.close();

}

void saveMultip() {


  SD.remove("multiplier.txt");
  int savemultip = multiplier;
  muliplierFile = SD.open("multiplier.txt", FILE_WRITE);
  muliplierFile.print(savemultip);
  muliplierFile.close();

}

void saveTune1() {

  SD.remove("tune1.txt");
  int savetune1 = tune1;
  tune1file = SD.open("tune1.txt", FILE_WRITE);
  tune1file.print(savetune1);
  tune1file.close();

}

void saveTune2() {


  SD.remove("tune2.txt");
  int savetune2 = tune2;
  tune2file = SD.open("tune2.txt", FILE_WRITE);
  tune2file.print(savetune2);
  tune2file.close();

}

void saveTune3() {


  SD.remove("tune3.txt");
  int savetune3 = tune3;
  tune3file = SD.open("tune3.txt", FILE_WRITE);
  tune3file.print(savetune3);
  tune3file.close();

}

void saveTune4() {
  SD.remove("tune4.txt");
  int savetune4 = tune4;
  tune4file = SD.open("tune4.txt", FILE_WRITE);
  tune4file.print(savetune4);
  tune4file.close();
}

void saveTune5() {
  SD.remove("tune5.txt");
  int savetune5 = tune5;
  tune5file = SD.open("tune5.txt", FILE_WRITE);
  tune5file.print(savetune5);
  tune5file.close();
}

void offlineSaveStart() {
  String SendTotalSec = String (totalSec);
  String SendVolume = String (totalVolume);
  String SendDosage = String (Dosis);
  String SendResidue = String (Residu);
  //  String forSend = "start,Time:" + SendTotalSec + ",mlOut:" + SendVolume + ",Dosage:" + SendDosage + ",Residue:" + SendResidue;
  String forSave = "start/" + SendTotalSec + "/" + SendVolume + "/" + SendDosage + "/" + SendResidue;

  if (SD.exists("offlineSave.txt")) {
    offlineSaver = SD.open("offlineSave.txt");
    offlineSaver.println();
    offlineSaver.print(forSave);
    offlineSaver.close();
  } else {
    Serial.println("offlineSave file doesn't exist");
    Serial.println("creating File");
    delay(100);
    offlineSaver = SD.open("offlineSave.txt", FILE_WRITE);
    offlineSaver.print("|");
    offlineSaver.print(forSave);
    offlineSaver.close();
  }
}

void offlineSaveStop() {
  String SendTotalSec = String (totalSec);
  String SendVolume = String (totalVolume);
  String SendDosage = String (Dosis);
  String SendResidue = String (Residu);
  //  String forSend = "start,Time:" + SendTotalSec + ",mlOut:" + SendVolume + ",Dosage:" + SendDosage + ",Residue:" + SendResidue;
  String forSave = "stop/" + SendTotalSec + "/" + SendVolume + "/" + SendDosage + "/" + SendResidue;

  if (SD.exists("offlineSave.txt")) {
    offlineSaver = SD.open("offlineSave.txt");
    offlineSaver.println();
    offlineSaver.print(forSave);
    offlineSaver.close();
  } else {
    Serial.println("offlineSave file doesn't exist");
    Serial.println("creating File");
    delay(100);
    offlineSaver = SD.open("offlineSave.txt", FILE_WRITE);
    offlineSaver.println();
    offlineSaver.print(forSave);
    offlineSaver.close();
  }
}

void offlineSaveFinish() {
  String SendTotalSec = String (totalSec);
  String SendVolume = String (totalVolume);
  String SendDosage = String (Dosis);
  String SendResidue = String (Residu);
  //  String forSend = "start,Time:" + SendTotalSec + ",mlOut:" + SendVolume + ",Dosage:" + SendDosage + ",Residue:" + SendResidue;
  String forSave = "finish/" + SendTotalSec + "/" + SendVolume + "/" + SendDosage + "/" + SendResidue;

  if (SD.exists("offlineSave.txt")) {
    offlineSaver = SD.open("offlineSave.txt");
    offlineSaver.print("|");
    offlineSaver.print(forSave);
    offlineSaver.close();
  } else {
    Serial.println("offlineSave file doesn't exist");
    Serial.println("creating File");
    delay(100);
    offlineSaver = SD.open("offlineSave.txt", FILE_WRITE);
    offlineSaver.print("|");
    offlineSaver.print(forSave);
    offlineSaver.close();
  }
}

void sendOfflinedata() {
  offlineSaver = SD.open("offlineSave.txt");
  String forSend = String (offlineSaver.read());
  char chrForSend [forSend.length() + 1];
  forSend.toCharArray(chrForSend, forSend.length() + 1);
  client.publish(msg_PUB_SEND_TIME, chrForSend, 0);
  delay(100);
  offlineSaver.close();
  SD.remove("offlineSave.txt");
}

void checkMaxResidue() {
  File chkMaxResidue;
  String strMaxResidu;
  if (!SD.begin(4)) {
    //    int sdtrying = 0;
    Serial.println("Initializing Failed");
    dataSeparator();
    //    sdtrying++;
    return;
  }

  if (SD.exists("chkMaxResidue.txt")) {
    Serial.println("chkMaxResidue file exist");
    //    dataSeparator();
  } else {
    Serial.println("chkMaxResidue file doesn't exist");
    Serial.println("creating File");
    delay(1000);
    chkMaxResidue = SD.open("chkMaxResidue.txt", FILE_WRITE);
    chkMaxResidue.print("1000");
    chkMaxResidue.close();
    //    dataSeparator();
  }
  //  Serial.println("disini");
  chkMaxResidue = SD.open("chkMaxResidue.txt");
  if (chkMaxResidue) {
    while (chkMaxResidue.available()) {
      char ltrMaxResidue = chkMaxResidue.read();
      strMaxResidu += ltrMaxResidue;
    }
    maxResidue = strMaxResidu.toInt();
    chkMaxResidue.close();
    //    Serial.println("disana");
  } else {
    Serial.print("error Opening lastResidue");
  }
}

void savemaxResidue(int val) {

  File filemaxResidue;

  SD.remove("chkMaxResidue.txt");
  int saveMaxResidue = val;
  filemaxResidue = SD.open("chkMaxResidue.txt", FILE_WRITE);
  filemaxResidue.print(saveMaxResidue);
  filemaxResidue.close();

  maxResidue = val;
  Residu = maxResidue;
  intResidue = Residu;

  ResidueSave();

}

Perhaps the problem is in your code that you have not posted