Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

Use of CMap blows up swap file

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
aderounm
Asked:
aderounm
  • 3
1 Solution
 
snoeglerCommented:
Why do you have deleted your previous question?
0
 
aderounmAuthor Commented:
Edited text of question
0
 
aderounmAuthor Commented:
Edited text of question
0
 
Answers2000Commented:
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
 
aderounmAuthor Commented:
Saved! I wonder how I missed that. I was about 48 hours away from  buying a new hard drive.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now