473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

memory managers and malloc/free

Hello,

I've read in the FAQ that modern versions of malloc/free can cache
memory, i.e. free does not return memory to the OS so that it may be
re-used by the next malloc call. I was thinking about writing some kind
of high level memory manger for a scientific project, but then I may
just be better off with malloc/free ... .

Here's the background: I need to allocate lots of vectros, matrices and
structures iteratively, specificly - I loop over a list of many data
sets and compare each of the sets with a list of other dataset. The
dataset are spectra with 2 double vectors of datapoints that were
recorded from a mass spectrometer, the spectra are of different size
(30 to 7000 data points in each vector).

For each comparsion I need to allocate a matrix, some vectors and
structures. So I was thinking to implement some kind of thread safe
(I'm planing to run the comparisons in parallel) memory manager that
will keep the matrices, vectors and structures so that they can be
re-used for the next round of comparsion (the vectors and matrices may
need to be re-sized, or I just increase but never decrese them). The
content of the memory can get lost after each round, it's just
important that memory allocation is fast (I've several mio.
comparsions).

Anyway, I'm not sure whether such a memory manager makes much sense if
malloc/free do caching, or maybe there are already efficient caching
systems available. What's your recommendation or expereince with large
numbers of malloc/free calls?

I don't have much experience in C, so even trivial answers may help ;-)
..

(I'm using gcc 2.96 under linux and the standard c-compiler on sun.9

thanks a lot for your help
+kind regards,

Arne

Aug 12 '06 #1
3 1529
ar*********@gmail.com wrote:
Hello,

I've read in the FAQ that modern versions of malloc/free can cache
memory, i.e. free does not return memory to the OS so that it may be
re-used by the next malloc call. I was thinking about writing some kind
of high level memory manger for a scientific project, but then I may
just be better off with malloc/free ... .
[...]
I don't have much experience in C, so even trivial answers may help ;-)
"You'd just be better off with malloc/free."

Don't optimize prematurely: you are likely to wind up chasing
imaginary problems and ignoring the real ones. This isn't a slam
at your inexperience; even expert programmers have been found to
be startlingly unable to predict which parts of their programs
will be the performance bottlenecks.

Begin by writing a straightforward program using straightforward
techniques. Work with the program until you have confidence that it
operates correctly. Then *if* the performance is inadequate and *if*
profiling and other measurement techniques show that a lot of time is
wasted in malloc/free -- then and only then should you start thinking
about customized wrappers and replacements.
(I'm using gcc 2.96 under linux and the standard c-compiler on sun.9
Aside: If performance is important to you, why are you using such
antique compilers? gcc is up to 4-dot-something, and the Sun Studio
compiler and tool set is free for the downloading. Compiler version
number does not always correlate positively with program speed, but
it's eccentric to insist on versions that were already old when
Clinton was elected.

--
Eric Sosman
es*****@acm-dot-org.invalid

Aug 12 '06 #2
On 2006-08-12 14:36:10 -0400, ar*********@gmail.com said:
Hello,

I've read in the FAQ that modern versions of malloc/free can cache
memory, i.e. free does not return memory to the OS so that it may be
re-used by the next malloc call. I was thinking about writing some kind
of high level memory manger for a scientific project, but then I may
just be better off with malloc/free ... .
Odds are malloc/free will do just fine for you; it is almost never a
good idea to second-guess your compiler/OS/libraries unless you
actually observe something amiss.
--
Clark S. Cox, III
cl*******@gmail.com

Aug 12 '06 #3
Ark
Eric Sosman wrote:
ar*********@gmail.com wrote:
>Hello,

I've read in the FAQ that modern versions of malloc/free can cache
memory, i.e. free does not return memory to the OS so that it may be
re-used by the next malloc call. I was thinking about writing some kind
of high level memory manger for a scientific project, but then I may
just be better off with malloc/free ... .
[...]
I don't have much experience in C, so even trivial answers may help ;-)

"You'd just be better off with malloc/free."

Don't optimize prematurely: you are likely to wind up chasing
imaginary problems and ignoring the real ones. This isn't a slam
at your inexperience; even expert programmers have been found to
be startlingly unable to predict which parts of their programs
will be the performance bottlenecks.
<snip>

Even if you find a necessity of custom memory manager after heeding to
Eric's very sound advice, do not rush to writing your own, unless you
KNOW your memory usage patterns that only you can take advantage of.
There are a few known approaches worth googling; customalloc is one of them.
- Ark
Aug 15 '06 #4

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

Similar topics

6
by: benevilent | last post by:
Hey, I'm trying to debug the memory allocation in an embedded use of the Python interpreter. The longer I leave my program running the more memory it consumes. The total number of objects in...
46
by: sbayeta | last post by:
Hi, I'd like to know who is responsible of memory recycling and defragmentation in a C/C++ program, assuming all the memory allocation/deallocation is done using malloc/free or new/delete. ...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
5
by: RoSsIaCrIiLoIA | last post by:
why not to build a malloc_m() and a free_m() that *check* (if memory_debug=1) if 1) there are some errors in bounds of *all* allocated arrays from them (and trace-print the path of code that make...
3
by: uday.das | last post by:
hi , I am not sure but I think it might be to avoid heap fragmentation due to several malloc calls. Are there any strong reason ? What are the other things that should take care when we implement...
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...
6
by: Dave Rahardja | last post by:
I need to design a container that must hold a set of references to a variety of object types, managed by different memory managers. At some time, the container must destroy each object. Currently I...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.