DECLARE @OS_TotalMemory INT
		,@OS_CPUCount INT
		,@OS_Reserved INT
		,@SQL_MAlloc INT
		
SELECT @OS_TotalMemory = CAST(total_physical_memory_kb AS INT) / 1024 / 1024 FROM sys.dm_os_sys_memory
SELECT @OS_CPUCount = cpu_count FROM sys.dm_os_sys_info
SELECT @OS_Reserved = 4
SELECT @SQL_MAlloc = ((@OS_TotalMemory - (@OS_CPUCount * 0.5) - @OS_Reserved) - (@OS_TotalMemory - (@OS_CPUCount * 0.5) - @OS_Reserved) % 2) * 1024

EXEC sp_configure 'max server memory', @SQL_MAlloc
RECONFIGURE