priitj / whitedb

WhiteDB memory database
http://whitedb.org/
GNU General Public License v3.0
608 stars 78 forks source link

wg_database_freesize result never decreases #46

Open vividsnow opened 5 years ago

vividsnow commented 5 years ago

Hello First of all, thanks for this project.

I'm trying to implement cache on top of whitedb, but found that even after deleting all records, wg_database_freesize result never decreases. Is there way to estimate real free size?

priitj commented 5 years ago

Hi,

Short answer: it's possible, but expensive to calculate.

You can think of whitedb memory allocation as working on two levels. There are bigger chunks that hold one type of objects, such as integers and strings. When one chunk gets filled, another of the same type is allocated. This "chunk level" memory is cheap to keep track of and this is what the wg_database_freesize() reports. However, it is not necessary to free those chunks and the side effect is that the size reported by that function never decreases.

The objects inside the chunks are freed and the memory does get reused. But the information about the free objects is stored in a way that makes it expensive to add up all the available space.

If you're curious then you can look at the function wg_show_db_area_header() and the functions that it calls in Test/dbtest.c

For practical purposes the best way would be to implement bookkeeping inside the whitedb library. There are a limited number of functions that create and free objects in Db/dballoc.c.

vividsnow commented 5 years ago

ok, thanks