Solved

Use of CMap blows up swap file

Posted on 1998-12-08
5
571 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video discusses moving either the default database or any database to a new volume.

758 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