rewrite of memory detection code, should be fix #1909