CreateDIBSection in the application memory
-
Tuesday, February 28, 2012 7:40 PM
Hi,
In my application uses DIBs to cache bitmaps for very fast drawing.
I know that CreateDIBSection allocates memory on the kernel's shared memory, and this is where my problem begins: when the cache becomes big other program starts to crush including mine.
I saw that in GDI+ Bitmaps/TextureMap are stored in the application memory space,
Is there any way to use my application memory space for the DIBs? (using CreateFileMapping is not an option because it will make my application slow).Thanks,
Roey
All Replies
-
Wednesday, February 29, 2012 10:19 AMModeratorIn my understanding on the document the memory is system allocated if the hSection parameter is NULL, so I think we cannot let it user the application process space.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, February 29, 2012 10:30 AMCan you please provide more details about the System Memory? how can i much of it is free? and etc...
-
Thursday, March 01, 2012 5:01 AM
Roey wrote:>>In my application uses DIBs to cache bitmaps for very fast drawing.>>I know that CreateDIBSection allocates memory on the kernel's shared>memory, and this is where my problem begins: when the cache becomes>big other program starts to crush including mine.>>Is there any way to use my application memory space for the DIBs?>(using CreateFileMapping is not an option because it will make my>application slow).That's nonsense. What on earth leads you to think that CreateFileMappingwill have ANY effect on performance? Creating a file mapping backed by thepage file is exactly identical to allocating memory from the heap. It'sjust a hunk of memory.--Tim Roberts, timr@probo.comProvidenza & Boekelheide, Inc.
Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.- Marked As Answer by Roey Friday, March 02, 2012 11:12 AM
-
Thursday, March 01, 2012 7:48 AM
Hi Tim,
Thanks for you reply :)
I guess "backed" by the page file in the documentation is not that obvious... i thought it worked the same way as a regular file, but instead of a file on mine, the system page file (i also read in an article that this is the case).
But,
Maybe there is a reason after all it was decided to allocate the DIBs in the system's memory? (performance reason?)Thanks,
Roey
-
Friday, March 02, 2012 9:58 AMModerator
Sorry, I'm not a GDI aspect expert(I hope I become to be :)),
Maybe I'm not very understanding the document, I should admit that I cannot explain the "system memory".
But in some articles and books has the DIB section memory management discussion, I saw that the "DIB section is allocated from the application's virtual memory space, or memory-mapped file. The size is only limited by the virtual memory space and the free disk space."
And in this page, it also said that, "Without a DIB section, you have to create a DIB and a DDB",(the DDB memory is from the kernel mode address space).
And I think you can use the Memory-Mapped File[A memory-mapped file contains the contents of a file in virtual memory.]
All the content is in the virtual memory, so I think it will not affect your application performance.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Roey Friday, March 02, 2012 11:12 AM
-
Friday, March 02, 2012 11:12 AM
Thanks for the answer Mike.
At the end i realized that i just shouldn't use so many DIBs and i'm cleaning my textures cache every time the display changes (and also setup an emergency clean, if the creatdibsection fails).
Thanks guys!
-
Saturday, March 03, 2012 6:40 PMRoey wrote:>>I guess "backed" by the page file in the documentation is not that>obvious... i thought it worked the same way as a regular file, but>instead of a file on mine, the system page file (i also read in an>article that this is the case).Sort of. Let me see if I can build you a mental model.(Except for the exceptional cases of locked memory) **ALL** memory isbacked up on disk somewhere. Really, the basic principle of a virtualmemory system is that virtual memory lives on disk, and some pieces of ithappen to be resident in physical memory at any given time. If youallocate heap memory with malloc, and the system comes under memorypressure, that part of the heap is going to be swapped out to disk. Inthis case, it goes to the page file (hence "backed by the page file").When you load a DLL, exactly the same process occurs, except that thememory is "backed by" the DLL file. If memory pressure rises, the DLL willbe swapped out (although since the memory hasn't changed, nothing isactually written to the disk).Memory-mapped files are just a way to gain access to that exact mechanism.When you allocate a memory mapped file, the information lives in memoryuntil the system comes under memory pressure, at which point it might getswapped out (if it hasn't been touched). The advantages of a memory-mappedfile are that a single chunk of memory can be mapped into many processes atonce, and you can have the backing go to a named file of your choiceinstead of the page file.Memory-mapped files hook into the paging system, which is among the mosthighly optimized modules in the entire kernel.--Tim Roberts, timr@probo.comProvidenza & Boekelheide, Inc.
Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc. -
Sunday, March 04, 2012 8:10 AM
Thanks Tim!
I understand now,
Why do you think Microsoft is not backing up DIBs in virtual memory by default (like it does in GDI+ Bitmaps)?
Roey
-
Monday, March 05, 2012 1:21 PMModeratorYou're welcome.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Monday, March 05, 2012 1:37 PMWelcome? Is this Tim?
-
Tuesday, March 06, 2012 4:29 AMRoey wrote:>>Why do you think Microsoft is not backing up DIBs in virtual memory>by default (like it does in GDI+ Bitmaps)?I'm not sure that they aren't. GDI first tries to allocate DIBs ingraphics card offscreen memory, because that results in more efficientdrawing. If that's full, it falls back to system memory.--Tim Roberts, timr@probo.comProvidenza & Boekelheide, Inc.
Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc. -
Tuesday, March 06, 2012 7:32 AM
I had another idea about why they didn't use virtual memory:
Maybe they thought that it's better to retun null and notify the program the V/RAM is full and that the application should try to free some of it, so that performance won't be affected.Thank you very much for your time Tim!


