CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2006
    Location
    England
    Posts
    72

    [RESOLVED] Can anyone explain why a processes memory grows like this...?

    I am confused why the size of a process grows larger in size for lots of small memory allocations.

    For example, say I have this struct, which is 16 bytes (for a 32 bit build):

    Code:
    struct Person
    {
        int iID;
        int iAge;
        char * pForeName;
        char * pSurName;
    };
    If I allocate memory like this:
    Code:
    LPBYTE lpB = new BYTE[sizeof(Person) * 1000000];
    then my process grows to 16,48KB in size, which is what I expected. However if I allocate memory like this:
    Code:
        Person * lpPerson;
        for(int i = 0; i < 1000000; ++i)
            lpPerson = new Person;
    then the process grows to 78,656KB, which I don't understand.

    Additionally, I was surprised to find a vector acts more similarly to the first example. This code:
    Code:
        Person temp = { 0 };
        std::vector<Person> people;
        for(int i = 0; i < 1000000; ++i)
            people.push_back(temp);
    only increases the process memory to 16,892.

    Is anyone able to explain why the lots of small allocations grows the process memory to such a huge size and/or why a vector doesn't suffer with the same issue?

    Cheers,
    AnotherMuggle

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Can anyone explain why a processes memory grows like this...?

    Maybe because there is book keeping associated with each allocation, hence more allocations mean more book keeping. The vector probably does not have that issue because it uses memory akin to your first example (except that it periodically expands the capacity when the current capacity has been reached, such that the push_back still operates in amortised constant time), though it avoids creating the objects that are not yet part of the vector's size.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Can anyone explain why a processes memory grows like this...?

    Quote Originally Posted by AnotherMuggle View Post
    I am confused why the size of a process grows larger in size for lots of small memory allocations.
    Each one of those allocations requires management, and the heap manager needs to expand to keep track of them.

    Not only that, the runtime may allocate more memory per request for things such as debug checking, guard bytes, etc.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2006
    Location
    England
    Posts
    72

    Re: Can anyone explain why a processes memory grows like this...?

    Quote Originally Posted by laserlight View Post
    Maybe because there is book keeping associated with each allocation, hence more allocations mean more book keeping. The vector probably does not have that issue because it uses memory akin to your first example (except that it periodically expands the capacity when the current capacity has been reached, such that the push_back still operates in amortised constant time), though it avoids creating the objects that are not yet part of the vector's size.
    Quote Originally Posted by Paul McKenzie View Post
    Each one of those allocations requires management, and the heap manager needs to expand to keep track of them.

    Not only that, the runtime may allocate more memory per request for things such as debug checking, guard bytes, etc.

    Regards,

    Paul McKenzie
    Two Elite Members, two good answers, what more could I ask for. Thanks both!

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [RESOLVED] Can anyone explain why a processes memory grows like this...?

    Just because you ask for n bytes of allocated memory, this does not mean that Windows actually allocates n bytes. It only guarantees to either allocate at least n bytes or report failure. On my system if you look at the memory allocated, Windows is actually allocating the memory in chuncks of either 24 bytes or 32 bytes rather than the 16 bytes requested.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jun 2013
    Posts
    1

    Re: [RESOLVED] Can anyone explain why a processes memory grows like this...?

    phim sex vip free,down phim sex 3gp|tai phim sex

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured