source: trunk/Utilities/Miscellaneous/SmallerReallocate/SmallerReallocate.cpp

Last change on this file was 126, checked in by roman, 11 years ago
  • Property svn:keywords set to Id
File size: 1.4 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: SmallerReallocate.cpp 126 2012-09-29 20:35:25Z roman $
6
7#include "stdafx.h"
8#include <windows.h>
9#include <psapi.h>
10
11#pragma comment(lib, "psapi.lib")
12
13VOID PrintPrivateUsage()
14{
15        PROCESS_MEMORY_COUNTERS_EX Counters = { sizeof Counters };
16        ATLVERIFY(GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*) &Counters, sizeof Counters));
17        _tprintf(_T("PrivateUsage: %d MB\n"), Counters.PrivateUsage >> 20);
18}
19
20int _tmain(int argc, _TCHAR* argv[])
21{
22        PrintPrivateUsage();
23        VOID* ppvItemsA[256];
24        static const SIZE_T g_nSizeA1 = 1 << 20; // 1 MB
25        _tprintf(_T("Allocating %d MB\n"), (_countof(ppvItemsA) * g_nSizeA1) >> 20);
26        for(SIZE_T nIndex = 0; nIndex < _countof(ppvItemsA); nIndex++)
27                ppvItemsA[nIndex] = malloc(g_nSizeA1);
28        PrintPrivateUsage();
29        static const SIZE_T g_nSizeA2 = 4 << 10; // 4 KB
30        _tprintf(_T("Reallocating to %d MB\n"), (_countof(ppvItemsA) * g_nSizeA2) >> 20);
31        for(SIZE_T nIndex = 0; nIndex < _countof(ppvItemsA); nIndex++)
32                ppvItemsA[nIndex] = realloc(ppvItemsA[nIndex], g_nSizeA2);
33        PrintPrivateUsage();
34        VOID* ppvItemsB[256];
35        static const SIZE_T g_nSizeB1 = 16 << 10; // 16 MB
36        _tprintf(_T("Allocating %d MB more\n"), (_countof(ppvItemsB) * g_nSizeB1) >> 20);
37        for(SIZE_T nIndex = 0; nIndex < _countof(ppvItemsB); nIndex++)
38                ppvItemsB[nIndex] = malloc(g_nSizeB1);
39        PrintPrivateUsage();
40        return 0;
41}
42
Note: See TracBrowser for help on using the repository browser.