Results 1 to 5 of 5

Thread: Memory allocation and management

  1. #1
    Join Date
    Jun 2009
    Posts
    441

    Memory allocation and management

    I was recently confronted with the problem of the potential impact of dynamic allocation of memory on the performance of a program.

    I do not want my old crust, but at the time where I was taught C/C++ (between 96 and 99), I was clearly explained that the allocation is not free, and it must avoid getting on the fly especially if these data are of considerable size, such as images for example. So I got used to pre-allocate as much memory as possible knowing that I needed and then work mainly on pointers.

    Today one of my colleagues showed me his code for video playback using mainly references, so that each new image, it allocates a new object (ie approximately 640x480x4 bytes) to be destroyed once processed image then reallocated to the next image etc.

    Obviously I immediately convinced him, but he argued that the additional allowance was negligible, and indeed some rapid tests and naive like "infinite loop" did not deny the thing. So even if I am do not "elegant" to play with the memory, why not, but it still goes against everything I believe.

    So I ask myself questions: Is it wrong that I learned my lessons? Or the progress of hardware or compiler has eliminated what was a constraint at the time? Or there is a flaw in the memory allocation tests and there is indeed a problem with abuse of new/delete? (after all, a "hello world" infinite loop, this is not the same as a real program that brews up ...)

    Could you enlighten me on the low memory management subject?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Memory allocation and management

    I think what happens is that as delete is new in its loop without a hand, the block allocator reuses previously deleted object. And as what costs is the search + tagging block, thus fast enough. Manufactured by providing a test in a loop and then deallocating in another you'll see the difference.

    And 640x480x4 is not huge.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Memory allocation and management

    Then we must see what is compared. Compared to an addition or multiplication, a memory allocation is expensive. Compared to the image processing (presumably more than one calculation per pixel ...), this is no longer.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Memory allocation and management

    Clearly, 640x480x4 is 1.2MB. On a machine with 2GB of RAM its 0.05%, or that slab.

    At worst don't make allowances any way, you risk fragmenting your memory, which may penalize your access, because of cache-miss on the TLB. But I think it will pass completely internally, usually due to disk access (there are always) and/or communication network. When this is not Windows decides that by swapping the kernel because there is more than 95% of available resources.

  5. #5
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Memory allocation and management

    At time, memory allocation is best for static allocation simply because there have not allocation itself. When the variable is static, space is reserved in the binary variable and the space is automatically taken from the stack by a simple offset address.

    Obviously these 2 spaces are limited and we can not abuse it. It is better to try to reuse the already allocated memory when you know you will still be in needing it. The most powerful is always the most algo

    Otherwise we can only note that even for computationally intensive programs of up to 30% of time is spent in the dynamic allocation.

Similar Threads

  1. Replies: 4
    Last Post: 26-03-2012, 07:59 PM
  2. Memory allocation of new object
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 09-03-2010, 10:18 AM
  3. Memory allocation in C++
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 09:56 AM
  4. Memory allocation optimized bitwise
    By Ricky58 in forum Software Development
    Replies: 3
    Last Post: 07-05-2009, 10:53 AM
  5. SBSMonitoring/SQL Memory allocation error (run away Memory usage)
    By Dilbert in forum Small Business Server
    Replies: 1
    Last Post: 21-09-2005, 11:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,297,097.37367 seconds with 17 queries