|
| |||||||||
| Tags: c compiler, c language, infinite loop, memory allocation, memory management |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| ||||
| ||||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Memory allocation and management" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Memory allocation of new object | Miles Runner | Software Development | 5 | 09-03-2010 10:18 AM |
| Memory allocation in C++ | Prashobh Mallu | Software Development | 5 | 08-02-2010 09:56 AM |
| Memory allocation optimized bitwise | Ricky58 | Software Development | 3 | 07-05-2009 11:53 AM |
| inetinfo.exe memory allocation | cmjkeegan | Small Business Server | 5 | 14-02-2006 03:28 PM |
| SBSMonitoring/SQL Memory allocation error (run away Memory usage) | David Gill | Small Business Server | 1 | 21-09-2005 12:32 PM |