Use memory map file expansion programs available memory

zhaozj2021-02-11  195

Use memory map file expansion programs available memory

Windows uses a page swap file to expand the actual memory that the application is actually available, not limited to actual 32 megabits or 64 megabytes of physical memory. But Windows's page exchange file is shared by all programs, so our program wants to get more memory or more difficult, such as 600 trillion or more. Is there any way? The memory maps provided by Windows provide us with a solution to the problem.

Create a 600 megabyte file on the hard disk, then map it to the address space of our process, and Windows will return a pointer to this memory address, I can use this pointer like a 600 trillion, Windows will For us to do a good job from hard disk files to memory and switch from memory to hard disk files, just like we use Windows itself, you don't have to spend a little thought, and speed is very fast.

The specific method is as follows:

// First build a file on the hard disk, the access logo is readable and writable,

Hfile: = CreateFile (pchar (filename), generic_read or generic_write, 0, nil, open_always, file_attribute_normal, 0);

// Create a file mapping object, ifilesize specifies the size of the mapping file to be established, for example, 600 mega ifilesize = 1024 * 1024 * 600. When the files on the hard drive we open are not so big, Windows will automatically extend the file to the specified size.

HfileMap: = CreateFilemapping (Hfile, NIL, Page_Readwrite, 0, iFilesize, NIL);

// Map the file data to the address space of the process

PDATA: = MapViewoffile (FmapHandle, File_Map_Write, 0, 0, iFileSize);

Then we can make full use of Windows for our more than 600 megabytes. Of course, after using it, do some clearing work.

/ / Revoke the mapping from the address space of the process

UnmapViewoffile (PDATA);

// Close the file mapping object and open file

CloseHandle (HFILEMAP);

CloseHandle (HFILE);

转载请注明原文地址:https://www.9cbs.com/read-5555.html

New Post(0)