@ SuppressWarnings ( "resource" ) private static long getSizeOfPhysicalMemoryForLinux ( ) { BufferedReader lineReader = null ; try { lineReader = new BufferedReader ( new FileReader ( LINUX_MEMORY_INFO_PATH ) ) ; String line = null ; while ( ( line = lineReader . readLine ( ) ) != null ) { Matcher matcher = LINUX_MEMORY_REGEX . matcher ( line ) ; if ( matcher . matches ( ) ) { String totalMemory = matcher . group ( 1 ) ; return Long . parseLong ( totalMemory ) * 1024L ; // Convert from kilobyte to byte } } // expected line did not come LOG . error ( "Cannot determine the size of the physical memory using '/proc/meminfo'. Unexpected format." ) ; return - 1 ; } catch ( NumberFormatException e ) { LOG . error ( "Cannot determine the size of the physical memory using '/proc/meminfo'. Unexpected format." ) ; return - 1 ; } catch ( IOException e ) { LOG . error ( "Cannot determine the size of the physical memory using '/proc/meminfo': " + e . getMessage ( ) , e ) ; return - 1 ; } finally { // Make sure we always close the file handle try { if ( lineReader != null ) { lineReader . close ( ) ; } } catch ( Throwable t ) { } } }
Returns the size of the physical memory in bytes on a Linux - based operating system .