'How can you detect a dual-core cpu on an Android device from code?

I've run into a problem that appears to affect only dual-core Android devices running Android 2.3 (Gingerbread or greater. I'd like to give a dialog regarding this issue, but only to my users that fit that criterion. I know how to check OS level but haven't found anything that can definitively tell me the device is using multi-core.

Any ideas?



Solution 1:[1]

If you're working with a native application, you should try this:

#include <unistd.h>
int GetNumberOfProcessor()
{
    return sysconf(_SC_NPROCESSORS_CONF);
}

It work on my i9100 (which availableProcessors() returned 1).

Solution 2:[2]

You can try using Runtime.availableProcessors() as is suggested in this answer

Is there any API that tells whether an Android device is dual-core or not?

---edit---

A more detailed description is given at Oracle's site

availableProcessors

public int availableProcessors()

Returns the number of processors available to the Java virtual machine.

This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.

Returns:

the maximum number of processors available to the virtual machine; never smaller than one

Since:

  1.4

Solution 3:[3]

This is pretty simple.

int numberOfProcessors = Runtime.getRuntime().availableProcessors();

Typically it would return 1 or 2. 2 would be in a dual-core CPU.

Solution 4:[4]

Here's my solution, in Kotlin, based on this one:

/**
 * return the number of cores of the device.<br></br>
 * based on : http://stackoverflow.com/a/10377934/878126
 */
val coresCount: Int by lazy {
    return@lazy kotlin.runCatching {
        val dir = File("/sys/devices/system/cpu/")
        val files = dir.listFiles { pathname -> Pattern.matches("cpu[0-9]+", pathname.name) }
        max(1, files?.size ?: 1)
    }.getOrDefault(1)
}

Solution 5:[5]

I use a combination of both available solutions:

fun getCPUCoreNum(): Int {
  val pattern = Pattern.compile("cpu[0-9]+")
  return Math.max(
    File("/sys/devices/system/cpu/")
      .walk()
      .maxDepth(1)
      .count { pattern.matcher(it.name).matches() },
    Runtime.getRuntime().availableProcessors()
  )
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thai Phi
Solution 2 Community
Solution 3 DanKodi
Solution 4
Solution 5