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

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

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.