How to programmatically check if an allocation is possible

Could you check the system free memory like here?