Code uploads fine but serial monitor wont open

Hi, I was doing some debugging on some code but when i tried to upload it it says uploaded successfully but when i try to open serial monitor it gives an error stating "Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space". So i tried to upload a code which i know works properly and has no issues but still the problem remains the same can some one help me in figuring this out?

Here is the code which i know works properly

#include <Servo.h>

#define numOfValsRec 5
#define digitsPerValRec 1

Servo servoThumb;
Servo servoIndex;
Servo servoMiddle;
Servo servoRing;
Servo servoPinky;
int valsRec[numOfValsRec];
int stringLength = numOfValsRec * digitsPerValRec + 1;
int counter = 0;
bool counterStart = false;
String recievedString;

void setup() {
void recieveData(){
  while (Serial.available())
    char c =;
    if (c == '$'){
     counterStart = true;
    if (counterStart) {
      if (counter < stringLength) {
        recievedString = String(recievedString + c);
        counter ++;
      if (counter >= stringLength) {
        for (int i = 0; i < numOfValsRec; i++)
          int num = (i * digitsPerValRec) + 1;
          valsRec[i] = recievedString.substring(num, num + digitsPerValRec).toInt();
        recievedString = "";
        counter = 0;
        counterStart = false;
void loop() {

  if(valsRec[0] == 1){servoThumb.write(180);}else{servoThumb.write(0);}
  if(valsRec[1] == 1){servoIndex.write(180);}else{servoIndex.write(0);}
  if(valsRec[2] == 1){servoMiddle.write(180);}else{servoMiddle.write(0);}
  if(valsRec[3] == 1){servoRing.write(180);}else{servoRing.write(0);}
  if(valsRec[4] == 1){servoPinky.write(180);}else{servoPinky.write(0);}

EDIT:- after tying few more times to upload different codes it somehow miraculously fixed itself don't know what happened in the first place or how it got fixed but it looks like it is working for now

Good that it came right.

Next time please provide some details like operating system, IDE version and whatever else you think can be relevant.