Calling Ethernet.begin() anywhere in sketch causes display.begin() to fail.

I am attempting use an Ethernet shield (Rev3, W5500), with an I2C display, using the Adafruit_SSD1306 library.

I call the display.begin() from from the Adafruit_SSD1306 library as the first line in my setup() function. However, if I call Ethernet.begin() anywhere else in setup (I’m calling it several lines later), the display.begin() call fails and returns false, and the display does not function.

Simply commenting out Ethernet.begin() will cause display.begin() to work correctly.

Has anyone else encountered this, and have any solutions?

Here is my test sketch:

#include <Adafruit_SSD1306.h>

#include <Ethernet.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_ADDR   0x3C // OLED display TWI address

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

bool displayEnabled = false;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  // initialize and clear display
  displayEnabled = display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  Serial.begin(9600);
  Serial.print("Display enabled? : ");
  Serial.println(boolToString(displayEnabled));
  display.clearDisplay();
  display.display();

  Ethernet.begin(mac);

  // display a line of text
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Hello, world!");
  
  // update display with all of the above graphics
  display.display();
}

char* boolToString(bool val) {
  return val ? "true" : "false";
}

void loop() {
}

Clearly the first begin call cannot fail because of something that happens later in time, so this must be
out of memory or some such issue relating to space/resources.

Just having the call in the program will mean the library code gets linked in, and I think
here the memory requirements for both libraries might be exceeding the amount of SRAM
available, causing memory corruption?

Thanks. Now you mention it, the sketch does leave only 1132 bytes of dynamic memory, and a 128x64 bit buffer alone would use 1024 bytes, leaving very little memory remaining.

After testing, I can confirm: after reducing display size to 128x32 the display initializes successfully, but ethernet fails.

The standard ethernet buffer size is 1536 bytes, does the Ethernet library allow you to change this?