How to get maximum number of reclaimable bytes in windows

99 views Asked by At

In windows os i want to get maximum reclaimable bytes, I am using shrink querymax command in diskpart tool to get maximum reclaimable bytes but that taking more time, is there way to get same using c++ instead of depending on tool

2

There are 2 answers

1
doctorlove On

There are Win32 APIs, like IVdsVolumeShrink::QueryMaxReclaimable See here

They are Windows specific rather than portable C++.

0
Ozichi Orie On

Use this:

#include <windows.h>
#include <psapi.h>
#include <iostream>

int main() {
    PROCESS_MEMORY_COUNTERS pmc;

    if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
        SIZE_T peakWorkingSetSize = pmc.PeakWorkingSetSize;
        SIZE_T currentWorkingSetSize = pmc.WorkingSetSize;

        std::cout << "Peak Working Set Size: " << peakWorkingSetSize << " bytes\n";
        std::cout << "Current Working Set Size: " << currentWorkingSetSize << " bytes\n";

        // Calculate reclaimable bytes
        SIZE_T reclaimableBytes = peakWorkingSetSize - currentWorkingSetSize;
        std::cout << "Reclaimable Bytes: " << reclaimableBytes << " bytes\n";
    } else {
        std::cerr << "Error getting process memory information.\n";
    }

    return 0;
}

The difference between the peak and current working set sizes can give you an estimate of the maximum number of reclaimable bytes.