Solved

Use of CMap blows up swap file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 53
Expand macro to ask for filename column 8 37
sum28 challenge 31 106
fix34  challenge 9 104
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

23 Experts available now in Live!

Get 1:1 Help Now