Solved

CreateFileMapping granularity

Posted on 1998-03-26
16
504 Views
Last Modified: 2013-12-03
Is it true that every time I issue CreateFileMapping with -1 as the file handle I allocate 64KB on a paging-file???
I found a notion to that in Richter's book but could not found any concrete evidence.
If it is true, is there any way around writing a private memory manager (assuming I need many varying chunks of shared memory)?
0
Comment
Question by:gilg
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1412285
I don't know about the 64K issue.  Never heard of it.

However the smallest chunk that could be shared is 4K because the memory pages are 4K.  So if the OS is going to be giving you 64K its is giving you 16 times the minimum that is possible.  That is wastefull, but not increadibly so.  How many "chunks"s are you really going to have and how large are they really going to be.  If it is only a few dozen I wouldn't worry about it.   If it is hundreds, I'd rethink things a bit.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1412286
The documentation says:
If hFile is (HANDLE)0xFFFFFFFF, the calling process must also specify a mapping object size in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. The function creates a file-mapping object of the specified size backed by the operating-system paging file rather than by a named file in the file system. The file-mapping object can be shared through duplication, through inheritance, or by name.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412287
I think gilg's question is that despite what size you request, does it always use at least 64K?  It certainly uses at least 4k if you request less than 4k.
0
 
LVL 1

Accepted Solution

by:
dabbler earned 200 total points
ID: 1412288
Yes, you must allocate with a granularity of 64K.  This is an OS requirement, and there is no workaround.  Note that the 64K granularity is actually determined by calling GetSystemInfo () [sample code shown].  On all extant Windows OS's, the value is 64K, however.

DWORD GetAllocGran (void)
{
DWORD dwRet;
SYSTEM_INFO si;
memset (&si, 0x00, sizeof (si));
GetSystemInfo (&si);
dwRet = si.dwAllocationGranularity;
return (dwRet);
}

0
 

Author Comment

by:gilg
ID: 1412289
dabbler, I need some hard document proof to that. The only reference I found was a not so clear sentence in Richter's "Advanced Windows".
I'd expect the help on CreateFileMapping to have a large font 'WARNING: Each time you issue me I take up at least 64K on your swap file', wouldn't you?
So where do you get your information from?
Thanks
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412290
From the VC++ online help for GetSystemInfo()
**********************************************************************
dwAllocationGranularity

Specifies the granularity with which virtual memory is allocated. For example, a VirtualAlloc request to allocate 1 byte will reserve an address space of dwAllocationGranularity bytes. This value was hard coded as 64K in the past, but other hardware architectures may require different values.
*********************************************************************
This suggests that the granularity is currently 64K but they may change it in the future (or on different OS"s).  If you are hoping that they will decrease the size in the future you haven't been paying attention for the last 10 years.  If it changes, it probably won't be smaller.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412291
Note that 64K is not rally all thay much (these days).  You swap file is at least a meg and in probably much more, usually sever times the RAM in your machine.  Even if you are only on an 8 meg machine.  (Well below the minimum required to run office 97!) your swap file is probably at least 16 meg.   How many mappings are you going to be creating? If you create a 100 such mappings that's 640K out of 16 meg.  So about 1/32 of the swap file is being used.  That is not great, but it is not terrible.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412292
Sorry for the typos!  Hopefully you can understand what I said.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:dabbler
ID: 1412293
See nietod's comment for the documentation (thanks, nietod! :) ).  I also read the info in Richter's book, too.
0
 

Author Comment

by:gilg
ID: 1412294
And no software kits for memory management? I would think someone will have the need.
I'm allocating at least a thousand chunks. Most of them very small but I don't want to manage my memory myself because they change often (you know segmentation and all that crap).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412295
I think you need to rethink your design.  For a 1000 allocations you will be using 6.4 meg of virtual memory.  That might have a serious impact upon performance.  (Although it probably will work.)   What is it that your are trying to do?
0
 

Author Comment

by:gilg
ID: 1412296
It IS working. The problem is I already wrote the stuff. This is a DLL that supplies Dictionary services. I wanted the dictionaries themselves to reside in shared memory so they can be passed from a process to another just by passing an handle. I used CreateFileMapping for each new dictionary. Now I'll have to change the whole I guess.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412297
I don't understand why you will need something like a 1000 mappings.  You should be able to create a few mappings (potentially one) and use different regions in the mappings.
0
 

Author Comment

by:gilg
ID: 1412298
That is what I ment when I asked about a memory management tool. If I create one mapping I must manage it somehow and I have a lot to think of like: defragmentation, reallocation with size changes and similar curses. I realy don't want to confront this mess in this stage of my life (I wrote this kind of stuff in the past but it was simpler and I was younger and full of energy then).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412299
You could write a very inneficient (simple) memory manager and still get a huge memory savings (and therefore a likely performance increase) over your current design.  Without knowing more details I can't say much more.
0
 

Author Comment

by:gilg
ID: 1412300
Well, I think that sums up my options. I guess I'll change my atitude and get on with it.
Thanks alot for the good advices.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now