Generally when you are out of memory, you really need to use another MCU with larger RAM.
But before doing that, you need to check if you are doing the “free” when the allocated memory is no more needed in your program, you also need to optimize the size of data’s (types/structures) : (eg. no need to use 32bits integer when your variable won’t exceed 0xFF)
Concerning -ffunction-sections & -fdata-sections : these options will not affect RAM usage, but they will affect ROM usage negatively. Your executable will be begger and slower !