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?
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:
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With