How to get the number of cores of an android device

You may use a combination of above answer with this one. This way it will perform faster on devices running API 17+ as this method is much faster than filtering out files.

private int getNumberOfCores() {
    if(Build.VERSION.SDK_INT >= 17) {
        return Runtime.getRuntime().availableProcessors()
    }
    else {
       // Use saurabh64's answer
       return getNumCoresOldPhones();
    }
}

/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 * @return The number of cores, or 1 if failed to get result
 */
private int getNumCoresOldPhones() {
    //Private Class to display only CPU devices in the directory listing
    class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            //Check if filename is "cpu", followed by a single digit number
            if(Pattern.matches("cpu[0-9]+", pathname.getName())) {
                return true;
            }
            return false;
        }      
    }

    try {
        //Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about
        File[] files = dir.listFiles(new CpuFilter());
        //Return the number of cores (virtual CPU devices)
        return files.length;
    } catch(Exception e) {
        //Default to return 1 core
        return 1;
    }
}

public int availableProcessors ()

Added in API level 1 Returns the number of processor cores available
to the VM, at least 1. Traditionally this returned the number
currently online, but many mobile devices are able to take unused
cores offline to save power, so releases newer than Android 4.2 (Jelly
Bean) return the maximum number of cores that could be made available
if there were no power or heat constraints.

Also there is information about number of cores inside a file located in
/sys/devices/system/cpu/present It reports the number of available CPUs in the following format:

  • 0 -> single CPU/core
  • 0-1 -> two CPUs/cores
  • 0-3 -> four CPUs/cores
  • 0-7 -> eight CPUs/cores

etc.

Also please check what is inside /sys/devices/system/cpu/possible on a note 3

Both files have r--r--r-- or 444 file permissions set on them, so you should be able to read them without a rooted device in code.

EDIT: Posting code to help you out

 private void printNumberOfCores() {
        printFile("/sys/devices/system/cpu/present");
        printFile("/sys/devices/system/cpu/possible");

    }

    private void printFile(String path) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);
            if (inputStream != null) {

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line;

                do {
                    line = bufferedReader.readLine();
                    Log.d(path, line);
                } while (line != null);
            }
        } catch (Exception e) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

With the result

D//sys/devices/system/cpu/present﹕ 0-3
D//sys/devices/system/cpu/possible﹕ 0-3

The test was run on OnePlus One running BlissPop ROM with Android v5.1.1 and it prints as it should. Please try on your Samsung

Leave a Comment