You are here: Advanced Features > Defragment > How To Use Defragmentation

How To Use Defragmentation

The simplest way to defragment a db4o file would be:

Defragment.Defrag("database.db4o");
DefragmentationExample.cs: Simplest possible defragment use case
Defragment.Defrag("database.db4o")
DefragmentationExample.vb: Simplest possible defragment use case

Ensure that the file is not opened by any other process or db4o instance.

This moves the file filename to filename.backup, then it create a defragmented version of this database at the original position. If the backup file already exists this an IOException is throw and no action will be taken.

You can also specify the backup filename manually:

Defragment.Defrag("database.db4o", "database.db4o.bak");
DefragmentationExample.cs: Specify backup file explicitly
Defragment.Defrag("database.db4o", "database.db4o.bak")
DefragmentationExample.vb: Specify backup file explicitly

For more detailed configuration of the defragmentation process, you can use a DefragmentConfig instance:

DefragmentConfig config = new DefragmentConfig("database.db4o");
Defragment.Defrag(config);
DefragmentationExample.cs: Defragment with configuration
Dim config As New DefragmentConfig("database.db4o")
Defragment.Defrag(config)
DefragmentationExample.vb: Defragment with configuration

It's possible to use a more fine grained configuration for the deframentation process. See "Defragmentation Configuration"

Defragmentation can throw IOException in the following situations: