Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserve disk space before writing a file for efficiency

Tags:

windows

I have noticed a huge performance hit in one of my projects when logging is enabled for the first time. But when the log file limit is reached and the program starts writing to the beginning of the file again, the logging speed is much faster (about 50% faster). It's normal to set the log file size to hundreds of MBs.

Most download managers allocate dummy file with the required size before starting to download the file. This makes the writing more effecient because the whole chunk is allocated at once.

What is the best way to reserve disk space efficiently, by some fixed size, when my program starts for the first time?

like image 344
haggag Avatar asked Mar 01 '10 15:03

haggag


People also ask

How much free space does a hard drive need to prevent a performance slowdown?

The 15% Rule of Thumb for Mechanical Hard Drives You'll commonly see a recommendation that you should leave 15% to 20% of a drive empty. That's because, traditionally, you needed at least 15% free space on a drive so Windows could defragment it.

How much space is allocated for a file?

To maintain efficiency in file system operations, the JFS allocates 4096 bytes of fragment space to files and directories that are 32KB or larger. A fragment that covers 4096 bytes of disk space is allocated to a full logical block.

How much space should be free in C drive?

50 GB of free space is absolutely enough for your C drive. But, if you are really afraid low drive space will cause some troubles for your computer, except using some ways to free up your drive space, you also can learn to extend your C drive with a third party partition manager.

How much free space does Windows need?

Windows 10 uses approximately 10 GBs of disk space. The minimum disk space available is to facilitate the installation process. Some install files are compressed and need to be expanded during setup. Some files are temporary and are deleted during or after installation.


1 Answers

void ReserveSpace(LONG spaceLow, LONG spaceHigh, HANDLE hFile)
{
    DWORD err = ::SetFilePointer(hFile, spaceLow, &spaceHigh, FILE_BEGIN);

    if (err == INVALID_SET_FILE_POINTER) {
        err = GetLastError();
        // handle error
    }
    if (!::SetEndOfFile(hFile)) {
        err = GetLastError();
        // handle error
    }
    err = ::SetFilePointer(hFile, 0, 0, FILE_BEGIN); // reset
}
like image 114
plinth Avatar answered Oct 10 '22 07:10

plinth