19

I can't get the total amount of data used by an Android Application (or package), because the official API support has been deleted:

http://groups.google.com/group/android-developers/browse_thread/thread/df94daae34336aad/f96a8425549c6637 (is this still the current situation?)

Getting installed app size (unsopported hack - do not use it)

So is there any way to get the current sqlite database size? I've found a getMaxSize method or getPageSize but I'm not able to find the total number of pages.

Community
  • 1
  • 1
Bedo
  • 925
  • 2
  • 14
  • 27

4 Answers4

36

Perhaps someone could verify if this is an acceptable approach, but for a while I was using this:

File f = context.getDatabasePath(dbName);
long dbSize = f.length();

Cheers

DroidBot
  • 361
  • 2
  • 2
  • this did it for me; in my opinion should be marked as answer – msysmilu Feb 02 '15 at 12:55
  • This won't include the `-journal` file size, which in case of [write-ahead-log](https://www.sqlite.org/wal.html) may also contain some committed data. – Alex Che Oct 08 '20 at 19:50
21

You can query the database with these two:

pragma page_size;
pragma page_count;

(see the sqlite pragma docs).

Multiply the first by the second and you'll get total pages used.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    This could work but I can't find a way to execute SQL code with a PRAGMA that returns something! Is there any way? – Bedo Jun 16 '11 at 20:41
  • 1
    Just run each one as if it was a query using RawQuery. (One at a time, no bind parameters). These should return one row. Some info [here](http://stackoverflow.com/questions/2675388/detect-if-a-table-contains-a-column-in-android-sqlite) with a different pragma. – Mat Jun 17 '11 at 04:43
  • 1
    I am getting different results using this method verses checking the length of the DB file. Sometimes and only on certain phones, this returns a larger size than the size of the DB file itself. I verified this by copying the DB file over to my computer. – Chris Feist Nov 01 '12 at 19:17
14

You can use this raw query as well, on sqlite 3.16.0+:

SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();

It returns the "size" column with the single row.

fche
  • 2,641
  • 20
  • 28
gerbit
  • 902
  • 9
  • 17
  • wow - I can't find these virtual tables documented anywhere… Do you have a link? – w00t Nov 06 '19 at 10:02
  • 1
    @w00t since 3.16.0, pragmas that return results and don't have side effects have been available as table-valued functions https://www.sqlite.org/pragma.html#pragfunc – Angus H Mar 15 '20 at 17:15
  • 1
    A useful point to note about this is that it returns the total DB size, including free pages, so deleting data wont reduce the size. To get the amount of used space you can do `SELECT (page_count - freelist_count) * page_size as size FROM pragma_page_count(), pragma_freelist_count(), pragma_page_size();` (Running `VACUUM` would recover the unused space and reduce the file size). – phil_rawlings Jun 18 '22 at 15:14
4

Just to add-on about the size.

When I used the code by DroidBot, it works. But the size may not straightaway increase when data is entered into the database (especially if the data is very small). Database will only increment in 1024 bytes (e.g from 8192 bytes to 9216 bytes).

So, to see the database size increase straightaway, try putting in a large amount of data.

Britc
  • 623
  • 5
  • 8