Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RAM page size?

I would like to optimize the reading of an InputStream, then I thought it would be good to have a byte[] buffer with the size of a RAM page.

Is there a method (possibly a static one) to know its size?

EDIT:

Finally I succeeded using NDK and JNI, I wrote the following code in C:

#include <jni.h>
#include <unistd.h>

jlong Java_it_masmil_tests_TestsActivity_pageSize(JNIEnv* env, jobject javaThis) {
    return sysconf(_SC_PAGE_SIZE);
}

where:

  • it.masmil.tests is the package name
  • TestsActivity is the class name
  • pageSize is the method name
  • env and javaThis are two mandatory parameters (usefull in some occasions)

I compiled that file with NDK, and then I wrote the following code in Java:

static {
    System.loadLibrary("clibrary");
}
private native long pageSize();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    long page = pageSize();
}

where:

  • clibrary is the name of the library I created with NDK
  • pageSize is the name of the method as declared in the C file
like image 706
Massimo Avatar asked Nov 12 '12 21:11

Massimo


1 Answers

Page size is defined by Linux (kernel) and you can get it via JNI by calling libc (bionic)'s sysconf(_SC_PAGESIZE). Since Android runs on Linux and mostly on ARM systems, you can assume 4k page sizes.

#include <unistd.h>
long sz = sysconf(_SC_PAGESIZE);

However you can't really get this kind of alignment from Java/C easily. Meaning even if you ask for a 4k block, no one guarantees that will be 4k aligned.

In case you need a 4k aligned block on Linux, you should use mmap which is guaranteed to be page size aligned.

like image 71
auselen Avatar answered Sep 19 '22 12:09

auselen