You are here: Configuration > File Configuration > Storage > Memory Storage

Memory Storage

The MemoryStorage allows you to create and use a db4o database fully in RAM. This strategy eliminates long disk access times and makes db4o much faster.

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
MemoryStorage memory = new MemoryStorage();
configuration.File.Storage = memory;
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, "database.db4o");
IOConfigurationExamples.cs: Using memory-storage
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
Dim memory As New MemoryStorage()
configuration.File.Storage = memory
IOConfigurationExamples.vb: Using memory-storage

MemoryStorage can be created without any additional parameters passed to the constructor. In this case default configuration values will be used.

PagingMemoryStorage

The regular MemoryStorage implementation keeps all the content in a single byte-array. However this brings some issues. When the database outgrows the array-size, a new, larger array is created and the content is copied over. This can be quite slow. Also can cause this a out of memory exception, because during the copying these two large arrays are present. Also, on some runtimes large objects are treated different by the garbage-collector and are less often collected.

To avoid all this issues, the PagingMemoryStorage uses multiple, small arrays to keep the database in memory. When the database outgrows the storage, only such a smaller arrays needs to be allocated. The old content stays in the existing arrays. No coping is required.

However managing these arrays cost some small overhead. But for lots of cases, the PagingMemoryStorage is the better choice.

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
PagingMemoryStorage memory = new PagingMemoryStorage();
configuration.File.Storage = memory;
IObjectContainer container = Db4oEmbedded.OpenFile(configuration, "database.db4o");
IOConfigurationExamples.cs: Using paging memory-storage
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
Dim memory As New PagingMemoryStorage()
configuration.File.Storage = memory
Dim container As IObjectContainer = Db4oEmbedded.OpenFile(configuration, "database.db4o")
IOConfigurationExamples.vb: Using paging memory-storage

Growth Strategy for MemoryStorage

Growth strategy defines how the database storage (reserved disk or memory space) will grow when the current space is not enough anymore.

DoublingGrowthStrategy - default setting. When the size of the database is not enough, the reserved size will be doubled.

ConstantGrowthStrategy - a configured amount of bytes will be added to the existing size when necessary.

IGrowthStrategy growStrategy = new ConstantGrowthStrategy(100);
MemoryStorage memory = new MemoryStorage(growStrategy);
configuration.File.Storage = memory;
IOConfigurationExamples.cs: Using memory-storage with constant grow strategy
Dim growStrategy As IGrowthStrategy = New ConstantGrowthStrategy(100)
Dim memory As New MemoryStorage(growStrategy)
configuration.File.Storage = memory
IOConfigurationExamples.vb: Using memory-storage with constant grow strategy

MemoryBin

Each memory storage can contain a collection of memory bins, which are actually just names memory storages. You can reuse the MemoryBin created earlier for this MemoryStorage. MemoryBins are identified by their URI, i.e. when an object container is opened with:

.NET:

Db4oEmbedded.OpenFile(embeddedConfiguration, "myEmbeddedDb.db4o");

A MemoryBin with URI = "myEmbeddedDb.db4o" will be used. If this memory bin does not exist in the storage when the container is opened, a new MemoryBin will be created and associated with this URI. When you pass the same memory storage to multiple object containers these containers can access to the same in memory file when they are using the same name.

More Reading: