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
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.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.