Solved

Use of CMap blows up swap file

Posted on 1998-12-08
5
582 Views
Last Modified: 2013-11-20
My program contains a CMap containing 300byte objects. When I load objects from a 600kb file (approx 2000 objects) into memory using CMap::Serialize() the windows swap file (win386.swp) blows completely out of proportion. I need about 2Gb free on my hard drive before the de-serializing works. The file contains approximately two thousand 300byte objects and I would expect the memory needed to be similar in size to the hard disk space needed which is only 600kb. My objects DO NOT allocate any extra memory! Can someone tell why the program needs so much memory.

   Update: The CMap default constructor is below:

   CMap( int nBlockSize = 10 );

   I have been constructing my CMaps with nBlockSize=4096. I assumed the BlockSize is in bytes. At
   this point I am beginning to wonder if it means the number of blocks of system memory. I think
   windows NT has a block size of either 2048 or 4096Bytes which means I may be allocating 4096
   blocks of 2048Bytes for each of the 2000 objects.
0
Comment
Question by:aderounm
  • 3
5 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1325916
Why do you have deleted your previous question?
0
 

Author Comment

by:aderounm
ID: 1325917
Edited text of question
0
 

Author Comment

by:aderounm
ID: 1325918
Edited text of question
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 900 total points
ID: 1325919
Read the MFC source

nBlockSize is set to an internal member of CMap (see AFXTEMPL.H) called m_nBlockSize

This is eventually used :-

CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize, sizeof(CMap::CAssoc));

If you read CPlex::Create in PLEX.CPP, you will see

CPlex* PASCAL CPlex::Create(CPlex*& pHead, UINT nMax, UINT cbElement)
{
      ASSERT(nMax > 0 && cbElement > 0);
      CPlex* p = (CPlex*) new BYTE[sizeof(CPlex) + nMax * cbElement];

Therefore the map allocates up in steps of

sizeof(Cplex) + ( BlockSize *  sizeof CAssoc )

* sizeof(CPlex) is just a few bytes
* CAssoc depends on what's in your map, the struct is also in AFXTEMPL.H
      struct CAssoc
      {
            CAssoc* pNext;
            UINT nHashValue;  // needed for efficient iteration
            KEY key;
            VALUE value;
      };

i.e. 8 + sizeof(KEY) + sizeof(VALUE).

So if your key + value are 300 bytes, then you can see the map increases in steps of =

few bytes + ( 4096 * 308 ) = 1.3 Mb

Basically what this means if you have a map of 0 elements, then you add one, then map growns big enough to allocate 4096 elements.  These get used up (removed from the free list) as you add more stuff, then another 4096 are allocated etc.

Which version of Windows/VC are you using by the way?


0
 

Author Comment

by:aderounm
ID: 1325920
Saved! I wonder how I missed that. I was about 48 hours away from  buying a new hard drive.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

823 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