In-Memory Database Size

884 views
Skip to first unread message

GregB

unread,
Jun 27, 2008, 1:29:58 PM6/27/08
to H2 Database
Is there any standard or H2 specific way to manage the size of an in-
memory database? My application has a defined memory limit and I need
to keep the DB size within this limit.

Right now my solution is to limit the number of rows in a table, and
when this is exceeded do a cascade delete to remove a row and all
dependents. This works well, however some databases the 1:M
relationship - M is typically 2 or 3, in other databases it may be
500. Right now I have to define the max rows for the worst case.

Is there any method to manage the database size other than a static
max rows value?


Thanks,

Greg

Thomas Mueller

unread,
Jul 1, 2008, 12:05:52 AM7/1/08
to h2-da...@googlegroups.com
Hi,

> Is there any standard or H2 specific way to manage the size of an in-
> memory database? My application has a defined memory limit and I need
> to keep the DB size within this limit.

No, there is no such feature at the moment. Could you explain the use
case please? Why do you need it, what exactly is the problem? I don't
understand fully when this feature would be useful. I will then add a
feature request, and depending how useful it is and how popular it is,
it will be implemented sooner or later. But first some more
information about the use case, advantages over alternatives and so on
is required. Maybe there is a workaround, or somebody could propose an
alternative solution.

Regards,
Thomas

GregB

unread,
Jul 1, 2008, 11:29:16 PM7/1/08
to H2 Database
Thomas,

Thanks for the reply - here is more details of my use case. I am
using H2 embedded in my application to store logging and diagnostics
information. The database is configured for in-memory storage. The
application has a max memory size specified at startup with –Xmx.

My goal is to store as much information as possible without causing an
OutOfMemory exception. The database schema has only 4 tables and 3 of
them foreign key to a master table. Right now to manage memory I have
defined a maximum number of rows for the master table, and when
removing a row I cascade the delete to the other tables.

The problem is that the application may be started with different –Xmx
settings depending on the server configuration. Also, the
relationship between a row in the master table and the other tables
may be somewhere between 1:1 and 1:500. A cascade delete may remove a
total of 4 rows or 1000 rows.

Setting a maximum number of rows cannot deal with dynamic nature of
the data. I have considered having an application timer that would
force a GC, check for free memory, and then remove rows if necessary.
Is there a better way to manage the database size?

Does H2 have the ability to configure a maximum memory footprint for
an in-memory DB? When the max is reached a callback is made to an
interface implemented by the application which can remove rows. Or
perhaps when max memory is reached all inserts would fail with a low
memory exception?


Thanks,

Greg


On Jun 30, 11:05 pm, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:

Thomas Mueller

unread,
Jul 7, 2008, 11:30:56 PM7/7/08
to h2-da...@googlegroups.com
Hi,

Sorry for the delay.

> When the max is reached a callback is made to an
> interface implemented by the application which can remove rows. Or
> perhaps when max memory is reached all inserts would fail with a low
> memory exception?

You could use the function MEMORY_FREE(), see
http://www.h2database.com/html/functions.html#memoryfree

> My goal is to store as much information as possible without causing an
> OutOfMemory exception.

I'm not sure if this a use case I like to support. There are many
problems, one is that Java does not support a lot of memory management
functions. Garbage collection details is not something you should rely
on. Calculating the used memory in H2 is not that easy: there is an
object cache, and if a value (VARCHAR, INT, DECIMAL,...) is already in
that cache no more memory is allocated except the pointer. I could
expose more cache management details (current number of cached rows,
estimated current cache size) but that wouldn't help you. If I was
you, I would try to solve the problem in another way.

> The database schema has only 4 tables and 3 of
> them foreign key to a master table. Right now to manage memory I have
> defined a maximum number of rows for the master table, and when
> removing a row I cascade the delete to the other tables.

Instead of using a database, could you use a SoftReference or
WeakReference hash map? Or an LRU map or set (it's easy, just extend
LinkedHashMap or LinkedHashSet)?

> Does H2 have the ability to configure a maximum memory footprint for
> an in-memory DB?

No.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages