Reputation: 11
I am using raspistill
to acquire images with my PI. after some weeks my SD card is full and the whole thing dies so I need to install again.
so I added some code to see what's happening with my available memory.
Here is the code to assess my memory status.
//https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.bpxbd00/rstatv.htm
int r = statvfs(".", &buf);
if (r < 0) {
printf(" Error in statsvf");
memAvailable = 4; // exit
}
else {
memAvailable = (unsigned long)(buf.f_bavail * buf.f_bsize);
printf(" Memory available is %.0lu\n", memAvailable);
}
this is the result I'm getting:
why is the available memory changing in the middle of the run mind you - I did not start or stop any program during this run, nor do I allocate a large size memory.
EDIT: My bad - it seems that 32GB is too long for an unsigned long. so i needed to assess only the f_bavail for my task. NOOB
Upvotes: 0
Views: 120
Reputation: 32596
You have an overflow when you do buf.f_bavail * buf.f_bsize
because the computation is done using an unsigned long
which not enough for the free size of your partition
Use unsigned long long
, for instance :
#include <sys/statvfs.h>
#include <stdio.h>
int main()
{
struct statvfs buf;
int r = statvfs(".", &buf);
if (r < 0) {
printf(" Error in statsvf");
}
else {
unsigned long long ull = buf.f_bavail * (unsigned long long) buf.f_bsize;
printf("Memory available is %llu\n", ull);
}
return 0;
}
You can also use uint64_t
to be sure to have a unsigned integer on 64b.
In my case :
pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
Memory available is 16778645504
pi@raspberrypi:/tmp $
and the result is compatible with the indication given by the command df -H .
Note to write memory about a partition on a disk is not very clear and can be confused with the RAM
Upvotes: 0