473,473 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Q: Allocating memory correctly

Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

thanks!
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #1
10 1832
Hi,
"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bq*************@news.t-online.com...
Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?
Typically new has to search for a free block that is large enough. Automatic
variables are usually faster (because it is just some space on the stack).
Try to use variables on the stack as much as possible. For variables that
take up a lot of space or that you just have to create dynamically use
dynamic allocation.

If it slows down your program you can always overload the new operator and
make a smarter (and likely more wasteful considering memory) implementation.

Regards, Ron AF Greve.
thanks!
--
jb

(replace y with x if you want to reply by e-mail)

Jul 22 '05 #2
"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Whenever allocating memory using operator new or operator new[] I feel like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

Typically new has to search for a free block that is large enough.

Automatic variables are usually faster (because it is just some space on the stack).
Try to use variables on the stack as much as possible. For variables that
take up a lot of space or that you just have to create dynamically use
dynamic allocation.


Hi,

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would. But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself or if using many vector's helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?) or if everything is fine the way I have
it.

thanks
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #3
Hi,

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bq*************@news.t-online.com...
"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Whenever allocating memory using operator new or operator new[] I feel like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?

Typically new has to search for a free block that is large enough.

Automatic
variables are usually faster (because it is just some space on the stack). Try to use variables on the stack as much as possible. For variables that take up a lot of space or that you just have to create dynamically use
dynamic allocation.


Hi,

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would. But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself or if using many vector's helps (not sure


I wouldn't start with that.
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?) or if everything is fine the way I have it.
If I where you I would use vector<> for arrays.

Then later on when your app actually does run too slow and an analysis of
your code shows it is due to new/delete you might invest some time in
overloading new/delete operator (do not release the memory for the class but
insert it into a linked list then when a new is done return the top of the
list).

Regards, Ron AF Greve.

thanks
--
jb

(replace y with x if you want to reply by e-mail)

Jul 22 '05 #4
Jakob Bieling wrote:

in my case I cannot use automatic variables, since the allocations are
either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would.
Those are good reasons to use the free store.
But since a lot of
those allocations might be done, I am asking myself if it is necessary to
write a memory manager myself
If you can write a memory manager that's better than the one that comes
with your compiler, by all means do it. <g> One advantage that you might
have is that you know the details of your application's memory usage. If
so, you might be able to do a better job for your application than the
general-purpose memory manager that comes with your compiler. But be
warned: memory managers are hard to write, and even harder to debug,
because the symptoms of a bug usually show up far from the point where
the actual error occurs.
or if using many vector's helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory
pool for the same kind of vectors?)
Maybe. But don't underestimate the capabilities of the memory manager
that comes with the compiler. They're often quite good.
or if everything is fine the way I have it.


If your application doesn't meet its requirements and you've determined
that the cause is the memory manager then you have to replace the memory
manager. If you application meets its requirements then everything is
fine the way you have it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #5
"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
Jakob Bieling wrote:

in my case I cannot use automatic variables, since the allocations are either an array of unknown size (but at least 1 element) or the variable
needs to live longer than automatic variables would.


Those are good reasons to use the free store.
But since a lot of
those allocations might be done, I am asking myself if it is necessary to write a memory manager myself


If you can write a memory manager that's better than the one that comes
with your compiler, by all means do it. <g> One advantage that you might
have is that you know the details of your application's memory usage. If
so, you might be able to do a better job for your application than the
general-purpose memory manager that comes with your compiler. But be
warned: memory managers are hard to write, and even harder to debug,
because the symptoms of a bug usually show up far from the point where
the actual error occurs.
or if using many vector's helps (not sure
about its allocator, but I am guessing/hoping that it uses the same memory pool for the same kind of vectors?)


Maybe. But don't underestimate the capabilities of the memory manager
that comes with the compiler. They're often quite good.
or if everything is fine the way I have it.


If your application doesn't meet its requirements and you've determined
that the cause is the memory manager then you have to replace the memory
manager. If you application meets its requirements then everything is
fine the way you have it.

Thanks guys, I'll just use the default thingies and see how well it
performs. :)
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #6
On Sun, 7 Dec 2003 12:21:31 +0100, "Jakob Bieling" <ne*****@gmy.net>
wrote:
Hi,

Whenever allocating memory using operator new or operator new[] I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?


Yes it is. That's why the STL provides the means to substitute custom
allocators, and often package their own allocators. Typically, they
allocate memory in big chunks, arrays of uninitialized objects that
can be used for more quick and efficient allocation for small objects.
The standard allocator is notably slow performing in multithreading
and multiprocessing situations. STLport, I hear, have a very good
allocator.
Jul 22 '05 #7
"Dan W." wrote:

STLport, I hear, have a very good
allocator.


"Very good" depends on what your requirements are. The STLport allocator
is fast, but it doesn't release memory for use by the rest of the
application.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #8
In general, with C++, you should avoid dynamic memory allocations.
Dynamic memory allocation should be considered only after you have
determined that a local variable or a fixed size allocation will not
do the job.

Also, it might be better to use STL than direct dynamic allocations
in your code. STL will take care of allocating and freeing memory as
needed.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - System Architecture Design CASE Tool
Jul 22 '05 #9
Pete Becker wrote:
"Dan W." wrote:
STLport, I hear, have a very good
allocator.

"Very good" depends on what your requirements are. The STLport allocator
is fast, but it doesn't release memory for use by the rest of the
application.


darn - those nasty trade-offs ... :-)

I suppose it keeps us software engineers in jobs - oh wait a sec, some
engineers in jobs.

Jul 22 '05 #10
Gianni Mariani wrote:

I suppose it keeps us software engineers in jobs - oh wait a sec, some
engineers in jobs.


:-(

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
3
by: Tod Birdsall | last post by:
Hi All, The organization I am working for has created a new corporate website that used Microsoft's Pet Shop website as their coding model, and dynamically served up content, but cached each...
19
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
29
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? ...
12
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
2
by: Evolution445 | last post by:
Hi, I have a function that reads 17 bytes starting from &H7C2B84. Each time it's about to read it throws an AccessViolationException, which, if I read posts around the net correctly means that...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.