Saturday, January 9, 2016

Determine number of processor cores

Runtime.availableProcessors() 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.

Example:
package com.blogspot.android_er.androidnumberofcores;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int availableCores = Runtime.getRuntime().availableProcessors();
        TextView textViewNumOfCores = (TextView)findViewById(R.id.numofcore);
        textViewNumOfCores.setText("Available Processors: " + availableCores);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidnumberofcores.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/numofcore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


4 cores on Xiaomi Redmi 2

single core on Android Emulator

No comments: