Possible SRAM leak?

Hi! I made a constant current load with an Arduino Nano, SD card, MCP4725 DAC, INA219 current meter and an 128x64 SPI oled screen. After some time sampling the power from the INA219, the oled shows a lot of artifacts. I've used different oled libraries and all of them showed artifacts after a while, but writing to the SD card does behave like normal. I found a function online that should show you how much SRAM is available, which showed 415 bytes of 2kb is available, but artifacts still happen.
Without the SD card library, the oled behaves as expected.
What could be the problem?


#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_MCP4725.h>
#include <SdFat.h>
#include <EEPROM.h>
#define SD_CS_PIN 7
#include <SPI.h>

#define inputCLK 3
#define inputDT 2

word counter = 0; 
byte currentStateCLK;
byte previousStateCLK; 
unsigned long Tijd = 0; //maximaal 604.800 seconden
unsigned long start;
unsigned long starten;

char fileName[] = "Gesampled000.txt";
float vermogen;

SdFat SD;
File myFile;

Adafruit_INA219 ina219;
Adafruit_MCP4725 dac;
#define DAC_RESOLUTION    (9)

//U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI(/* cs=*/ 0, /* dc=*/ 10, /* reset=*/ 9);	// Arduboy (DevKit)
//U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, 10, 9)
//U8G2_UC1701_DOGS102_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
U8X8_SSD1306_128X64_NONAME_4W_HW_SPI u8x8(10, 9, 6);
//cs = 10//dc = 9

void RotaryEncoder(){
  currentStateCLK = digitalRead(inputCLK);
   if (currentStateCLK != previousStateCLK){ 
     if (digitalRead(inputDT) != currentStateCLK) { 
       counter -= 1;
       
     } else {
       counter += 1;
       
     }
        previousStateCLK = currentStateCLK; 
   }
   Serial.println(counter);
}

ISR(TIMER1_OVF_vect){
  Tijd++;
  myFile = SD.open(fileName, FILE_WRITE);
  myFile.println("");
  myFile.print("Seconden: ");
  myFile.println(Tijd);
  myFile.print("vermogen: ");
  myFile.println(vermogen, 3);
  myFile.close();
  
  vermogen = 0;
  TCNT1 = 40000; 
}

void vermogenBerekenen(){
  unsigned int waardeTijd = micros() - starten;
  starten = micros();
  float spanning = (ina219.getShuntVoltage_mV()/1000) + ina219.getBusVoltage_V() - 0.008; //shunt moet naar V,  nu in mVwe
  float current = ina219.getCurrent_mA()*0.002; //Shunt = 50 mOhm
  if(current < 0.003){current = 0.00001;}
  if(spanning < 0){spanning = 0;}
  vermogen += current * spanning * (waardeTijd*pow(10,-6));
  //mWh = (Watt * waardeTijd)*pow(10,-6);
  //mWhDisplay += mWh/3.6;  
}

void setup(void){  
  Wire.begin();
  dac.begin(0x62);
   pinMode (inputCLK,INPUT);
   pinMode (inputDT,INPUT);
   previousStateCLK = digitalRead(inputCLK);
   attachInterrupt(digitalPinToInterrupt(3), RotaryEncoder, CHANGE);

  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.begin();
  u8x8.setPowerSave(0);
    if (! ina219.begin()) {
    u8x8.drawString(0,10,"INA219 FAIL");
    while (1) {}
  }
    if(!dac.begin(0x60)){
      u8x8.drawString(0,10,"MCP FAIL");
      while(1){}
    }
    if (!SD.begin(SD_CS_PIN)) {
      while(1){
      u8x8.drawString(0,10,"Sd FAIL");
      }
  }
  else{
  byte n = EEPROM.read(0) + 1;
  EEPROM.write(0, n+1);
  sprintf(fileName, "Sample0%02d.txt",n);
  }

  u8x8.drawString(0,10,"OK!");
  delay(3000);
  u8x8.clearDisplay();
  start = millis();
    //Time interrupt elke 1 sec.
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 40000;
  TCCR1B |= (1<<CS12); //prescaler
  TIMSK1 |= (1 << TOIE1); //Enable timer interrupt, (TIMSK1 &= ~(1<<TOIE1) = disable timer)
}

int availableMemory() {
    // Use 1024 with ATmega168
    int size = 2048;
    byte *buf;
    while ((buf = (byte *) malloc(--size)) == NULL);
        free(buf);
    return size;
}

void loop(){
  vermogenBerekenen();
  //u8x8.setCursor(80, 0);
 // u8x8.println(counter);
  dac.setVoltage(counter, false);
  if(millis() - start > 200){
    u8x8.clearDisplay();
    u8x8.setCursor(0, 20);
    int SRAM = availableMemory();
    u8x8.println(SRAM);
    u8x8.setCursor(0, 0);
    u8x8.println(Tijd);
    start = millis();
  }
}

The SD library allocates 512 bytes of memory each time you open a file. That doesn't show up in the compile time summary. Likewise, most OLED libraries require a 1K buffer.

You need something with more memory that the ATmega328 to use both at the same time.

Try this text-only OLED library, which does not require the screen buffer: GitHub - greiman/SSD1306Ascii: Text only Arduino Library for SSD1306 OLED displays

Thank you for the reply, i'll try this library and see if it improved.

UPDATE:
Unfortunately, this library does not work with my specific 128x64 oled

Try U8X8. It comes installed with U8G2 and is also a text only way to write to the OLED.

EDIT: I see that you are already using U8X8, so memory consumed by the OLED library will be minimal. Other text-only libraries will not give any significant saving.

I think you should consider upgrading to Nano Every.

This is a bad idea. ISR functions should be short and simple, like incrementing your variable. They should never use long, complex functions like writing to SD card, printing to displays or serial monitor etc.

What is the purpose of using timer and ISR here? Could it be achieved with millis()?

If you must use the timer ISR, check in loop() if the variable had been incremented and write to the SD card if it has.

I might be on the wrong track but which SD module do you use? There are modules that don't release one of the pins of the SPI bus; this can interfere with the data for the display. I can't remember the details.