Low SRAM and programme instability

I have a programme which when run on a Uno goes unstable as there are only 480 bytes of sram for local variables. However if I add a filled and used global array that takes up 512 bytes then run it on a Leonardo so I still only have 480 bytes free then the programme will happily run. Is there an explanation somewhere that explains how the IDE works out how many bytes of global variables are used and how the number of bytes free might not be enough for local variables.

A manuual count of the global variables of the original programme only comes up with 260 bytes but I am using SPI, a cut down version of the Ethernet library plus SDFAT beta Wire, and New Liquid crystal libraries so I know they will take up quite a bit but I don't understand how much Sram is counted for them.

It would be helpful if you post your program. We might be able to help you optimize it in order to solve your memory issues.