Low level memory allocation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Low level memory allocation

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,256

    Low level memory allocation

    I found myself reading this article at StackOverflow when I encountered a problem in our code with a variable that we'd allocated using boost::fast_pool_allocator. It turned out that the problem was in our own code so the boost connection was just a red herring but it got me wondering....

    One of the commentators in that article mentions that performance-wise, boost::fast_pool_allocator isn't particularly fast and this got me wondering about how the Windows heap manager allocates memory. If I malloc (say) 3 x chars, surely the heap manager doesn't examine the heap each time to find the next available free byte? I'd imagine that when I first ask for a char, it allocates some kind of pool from which it can allocate later bytes much more quickly (i.e. exactly what boost::fast_pool_allocator does).

    That's only a guess but it seems like the kind of strategy tht a modern OS would probably employ. Just curious to know if that's what happens in practice?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,022

    Re: Low level memory allocation

    Quote Originally Posted by John E View Post
    If I malloc (say) 3 x chars
    If you malloc, CRT heap manager is in effect, not Windows. To get directly to Windows heap, you call HeapAlloc.
    Best regards,
    Igor

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,256

    Re: Low level memory allocation

    Well... either way, with the vast amounts of memory available to a modern PC, is a pooling strategy generally used these days or does each request still get treated as a one-off?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    Join Date
    Oct 2008
    Posts
    1,146

    Re: Low level memory allocation

    Quote Originally Posted by John E View Post
    performance-wise, boost::fast_pool_allocator isn't particularly fast
    the boost pool allocators are designed to be used when frequent uniform de/allocations of fixed size types are needed ( plus non-contiguous if fast_pool_allocator is used ). A different de/allocattion pattern will have a time and space penalty with respect to the usual heap allocators, so you should not use them interchangeably ( so, I don't think that the default allocator employs any free-list based pool mechanism for general de/allocations ).

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,022

    Re: Low level memory allocation

    Quote Originally Posted by John E View Post
    Well... either way, with the vast amounts of memory available to a modern PC,
    Come on, John. A tiny error in your custom allocation strategy, and you'll find that vast amount totally fragmented and wasted. Did you ever thought how many allocs/deallocs is done in modern OS in a second by all those hundreds of threads?

    is a pooling strategy generally used these days or does each request still get treated as a one-off?
    Pool is a way of organizing chaos to something controllable. Of course the control costs something. Of course universal solution cannot be optimal for any specific situation. Which means, when your particular task is too much specific, and the cost becomes too high, you have to invent some other strategy, less universal, but more efficient.

    If I malloc (say) 3 x chars, surely the heap manager doesn't examine the heap each time to find the next available free byte?
    Well, as far as I know CRT heap manager exactly does that.
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,976

    Re: Low level memory allocation

    malloc() calls the HeapAlloc() win api (at least it does for the recent versions of the Visual C runtime).

    it may not be true for old versions of the runtime or may not be true for other compilers.

    HeapAlloc uses a "smart" allocation system. How it works isn't documented in detail by MS and it depends on what version of windows you're using.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center