Discussion:
sconf() limitations and strange implementation
(too old to reply)
John Dallman
2018-08-24 09:55:54 UTC
Permalink
I'm porting a large mathematical modelling library, written in C (*not*
C++) to Android. Its main test harness on UNIX and Windows platforms is a
command-line program, and I'm intending to build that as a command-line
tool and run it in the ADB shell. This is much easier than making a normal
Android app out of it. There will be an Android-specific test app as well,
of course.

My command line tool needs to find out how much memory is available for its
use. As it isn't a conventional app, it has no way to receive the normal
memory low warnings for GUI apps. As the devices that it runs on are
dedicated to doing testing, and are locked away in a server room to keep
them away from prying fingers, I don't need to worry about changes in
available memory as other apps are started and shut down. So it looked as
if I could use classic UNIX sconf() to find out how much memory is
available.

For the total physical memory of the machine, this works: sconf(
_SC_PAGE_SIZE) * sysconf( _SC_PHYS_PAGES) gives me a figure that matches
/proc/meminfo's MemTotal figure.

For available memory, sysconf( _SC_PAGE_SIZE) * sysconf( _SC_AVPHYS_PAGES)
gives me the same size as the /proc/meminfo's MemFree figure. But that's
quite small, about 200MB on a 4GB device with no apps active on the GUI.

So I went looking for an sconf parameter that matches proc/meminfo's
MemAvailable figure. I didn't find it, but I did find the source for that
part of Bionic LibC, at
https://android.googlesource.com/platform/bionic/+/a186b2e/libc/bionic/sysconf.cpp,
and that revealed that the _SC_PHYS_PAGES and _SC_AVPHYS_PAGES queries are
implemented by parsing /proc/meminfo.

This seems odd: is there no Linux system call that could be used to
implement these sysconf queries? Is there a proper way of getting a size
equivalent to MemAvailable?

Thanks,

John
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgmc%3DT%2Brcv7K-WvO5rx2UiiNwS7xkwRfMOvRQWb3_pnmfg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Alex Cohn
2018-08-26 09:57:36 UTC
Permalink
See https://stackoverflow.com/questions/22843468/how-to-get-the-available-free-memory-from-adb#comment83735811_22847667

BR,
Alex Cohn
Post by John Dallman
I'm porting a large mathematical modelling library, written in C (*not*
C++) to Android. Its main test harness on UNIX and Windows platforms is a
command-line program, and I'm intending to build that as a command-line
tool and run it in the ADB shell. This is much easier than making a normal
Android app out of it. There will be an Android-specific test app as well,
of course.
My command line tool needs to find out how much memory is available for
its use. As it isn't a conventional app, it has no way to receive the
normal memory low warnings for GUI apps. As the devices that it runs on are
dedicated to doing testing, and are locked away in a server room to keep
them away from prying fingers, I don't need to worry about changes in
available memory as other apps are started and shut down. So it looked as
if I could use classic UNIX sconf() to find out how much memory is
available.
For the total physical memory of the machine, this works: sconf(
_SC_PAGE_SIZE) * sysconf( _SC_PHYS_PAGES) gives me a figure that matches
/proc/meminfo's MemTotal figure.
For available memory, sysconf( _SC_PAGE_SIZE) * sysconf( _SC_AVPHYS_PAGES)
gives me the same size as the /proc/meminfo's MemFree figure. But that's
quite small, about 200MB on a 4GB device with no apps active on the GUI.
So I went looking for an sconf parameter that matches proc/meminfo's
MemAvailable figure. I didn't find it, but I did find the source for that
part of Bionic LibC, at
https://android.googlesource.com/platform/bionic/+/a186b2e/libc/bionic/sysconf.cpp,
and that revealed that the _SC_PHYS_PAGES and _SC_AVPHYS_PAGES queries are
implemented by parsing /proc/meminfo.
This seems odd: is there no Linux system call that could be used to
implement these sysconf queries? Is there a proper way of getting a size
equivalent to MemAvailable?
Thanks,
John
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/53a132c5-4470-4986-b8e8-7d6b850334b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
John Dallman
2018-08-28 12:45:24 UTC
Permalink
I need to get it programatically, rather than in the ADB command-line
environment. After digging a bit further, it seems that there isn't a Linux
API like the BSD vmmanager, so I'll just parse /proc/meminfo.

Thanks,

John
Post by Alex Cohn
See
https://stackoverflow.com/questions/22843468/how-to-get-the-available-free-memory-from-adb#comment83735811_22847667
BR,
Alex Cohn
Post by John Dallman
I'm porting a large mathematical modelling library, written in C (*not*
C++) to Android. Its main test harness on UNIX and Windows platforms is a
command-line program, and I'm intending to build that as a command-line
tool and run it in the ADB shell. This is much easier than making a normal
Android app out of it. There will be an Android-specific test app as well,
of course.
My command line tool needs to find out how much memory is available for
its use. As it isn't a conventional app, it has no way to receive the
normal memory low warnings for GUI apps. As the devices that it runs on are
dedicated to doing testing, and are locked away in a server room to keep
them away from prying fingers, I don't need to worry about changes in
available memory as other apps are started and shut down. So it looked as
if I could use classic UNIX sconf() to find out how much memory is
available.
For the total physical memory of the machine, this works: sconf(
_SC_PAGE_SIZE) * sysconf( _SC_PHYS_PAGES) gives me a figure that matches
/proc/meminfo's MemTotal figure.
For available memory, sysconf( _SC_PAGE_SIZE) * sysconf(
_SC_AVPHYS_PAGES) gives me the same size as the /proc/meminfo's MemFree
figure. But that's quite small, about 200MB on a 4GB device with no apps
active on the GUI.
So I went looking for an sconf parameter that matches proc/meminfo's
MemAvailable figure. I didn't find it, but I did find the source for that
part of Bionic LibC, at
https://android.googlesource.com/platform/bionic/+/a186b2e/libc/bionic/sysconf.cpp,
and that revealed that the _SC_PHYS_PAGES and _SC_AVPHYS_PAGES queries are
implemented by parsing /proc/meminfo.
This seems odd: is there no Linux system call that could be used to
implement these sysconf queries? Is there a proper way of getting a size
equivalent to MemAvailable?
Thanks,
John
--
You received this message because you are subscribed to the Google Groups
"android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-ndk/53a132c5-4470-4986-b8e8-7d6b850334b9%40googlegroups.com
<https://groups.google.com/d/msgid/android-ndk/53a132c5-4470-4986-b8e8-7d6b850334b9%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgmdv9XO7JcxLmC%3D-27Pi8D-hRbD4chc%2BCXWuMzGd23AcQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...