We have been using MongoDB for a few weeks. One thing we could not understood is Pre-allocated file. Is there any way to check whether the pre-allocated file is currently used by MongoDB or not?

We have planned to implement "Quota File". So we need to ensure the pre-allocated file is currently used or not.

For Example, I have the database file as follows:

sgserver.0 ---> 16MB
sgserver.1 ---> 128MB
sgserver.2 ---> 256MB
sgserver.3 ---> 512MB

How do I check the pre-allocated file sgserver.3 is used by MongoDB (for example, this database is used by any BSON or its Un-used)?

  • What operating system are you using? – Derek Downey Jan 5 '12 at 15:52

I am not very familiar with MongoDB, but from this link:

As soon as a datafile starts to be used, the next one will be preallocated.

It seems to me that (on *nix at least), MongoDB will always have a datafile that is empty, or mostly empty. If it's writing heavy and filling up the file, the last file might have some data in it before a new empty file can be created.

Additionally, on Unix, mongod will preallocate an additional datafile in the background and do background initialization of this file. These files are prefilled with zero bytes.

So to determine if it's used, check the file for non-zero bytes.


Expanding on Derek's answer: the last datafile preallocated will be unused. Always. As soon as the first document is written to a pristine data file, a new datafile will be allocated in parallel. This is done in order to prevent unnecessary latency when the existing data files don't have sufficient (free) space for a write operation. So at the expense of disk space, performance is increased. Given the price of a few cents per GB, this is reasonable.

You might reduce the preallocated space needed for a new database by starting mongod with the --smallfiles option. However, as soon as the data file size for preallocated files reaches 2GB, the space advantage is eliminated.

Note: for non production environments, the preallocation of data files can be turned off altogether: see the docs for storage.preallocDataFiles for details. Use at your own risk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy