completed detect_mem for OSX, fixed overflow issues