CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <chrono> // For timing
using namespace std::chrono;
int main() {
size_t memory_to_allocate = 512 * 1024 * 1024; // Start with 512 MB (adjust as needed)
while (true) { // Loop for binary search
std::cout << "Attempting allocation of " << memory_to_allocate / (1024 * 1024) << " MB..." << std::endl;
try {
auto start = high_resolution_clock::now(); // Start timer
std::vector<char> large_vector(memory_to_allocate);
// Access a portion of the allocated memory (adjust as needed)
for (size_t i = 0; i < std::min((size_t)10 * 1024 * 1024, memory_to_allocate); i += 4 * 1024) {
large_vector[i] = 'A';
}
auto stop = high_resolution_clock::now(); // Stop timer
auto duration = duration_cast<milliseconds>(stop - start);
std::cout << "Allocation and access successful! Time taken: " << duration.count() << " ms" << std::endl;
// Increase allocation size for the next iteration (adjust increment as needed)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run