473,503 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

repeat malloc & memory fragmentation

Yo,

I'm running a subroutine that mallocs a 10 or so Mb, then frees it on
exit. Then when it's called again it mallocs/frees again, and so on
for maybe 100 calls.

Sometimes the code fails after running a long time - is it possible
that malloc needs a contiguous block of memory but my frequent mallocs
have fragged it all up and so the later mallocs fail. Or... am I just
a bad programmer ;)

Thanks for your input.
Jul 22 '05 #1
5 3390
spasmous wrote:

Sometimes the code fails after running a long time - is it possible
that malloc needs a contiguous block of memory but my frequent mallocs
have fragged it all up and so the later mallocs fail.


Yes malloc will require a contigous block of memory.

Jul 22 '05 #2
On Sun, 01 Feb 2004 19:41:06 +0000 in comp.lang.c++, lilburne
<li******@godzilla.net> was alleged to have written:
spasmous wrote:

Sometimes the code fails after running a long time - is it possible
that malloc needs a contiguous block of memory but my frequent mallocs
have fragged it all up and so the later mallocs fail.


Yes malloc will require a contigous block of memory.


But it will more often be found that the program has a memory leak
(forgetting to free something that should be) and is actually running
out of available.

Jul 22 '05 #3
David Harmon wrote:
On Sun, 01 Feb 2004 19:41:06 +0000 in comp.lang.c++, lilburne
<li******@godzilla.net> was alleged to have written:
spasmous wrote:
Sometimes the code fails after running a long time - is it possible
that malloc needs a contiguous block of memory but my frequent mallocs
have fragged it all up and so the later mallocs fail.


Yes malloc will require a contigous block of memory.

But it will more often be found that the program has a memory leak
(forgetting to free something that should be) and is actually running
out of available.


Maybe, but then he doesn't say that the program has started
paging prior to running out of memory.

Jul 22 '05 #4
On Sun, 01 Feb 2004 20:35:38 +0000 in comp.lang.c++, lilburne
<li******@godzilla.net> was alleged to have written:
But it will more often be found that the program has a memory leak
(forgetting to free something that should be) and is actually running
out of available.


Maybe, but then he doesn't say that the program has started
paging prior to running out of memory.


Maybe, but he doesn't say much. Is there any more reason to think the
program would resort to paging to satisfy memory requests when the heap
was full than it would when the heap is fragmented?

Jul 22 '05 #5
spasmous wrote:
Yo,

I'm running a subroutine that mallocs a 10 or so Mb, then frees it on
exit. Then when it's called again it mallocs/frees again, and so on
for maybe 100 calls.

Sometimes the code fails after running a long time - is it possible
that malloc needs a contiguous block of memory but my frequent mallocs
have fragged it all up and so the later mallocs fail. Or... am I just
a bad programmer ;)

Thanks for your input.


How about looking at what you're doing between the calls of malloc and
free? Maybe something in there is causing the problem. To convince
yourself, why not write a very simple program that just allocates and
frees 10 Meg of memory in a loop?

#include <cstdlib>

void allocate_stuff()
{
char *p = (char *)malloc( 10000000 );
free(p);
}

int main()
{
char *p;
for ( int i = 0; i < 100; ++i )
allocate_stuff();
}

This code does what you describe. Does it cause problems as you've
described them, or does it run smoothly?

Paul
Jul 22 '05 #6

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

Similar topics

18
6654
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
11
8525
by: Aire | last post by:
On some small-memory systems, is it true that "malloc()" is not supported by the C library? Why is 'dynamic memory allocation' so avoided on such systems? What are major problems of using...
3
5192
by: dsptechie | last post by:
I wanted to how exactly malloc() function works. I came to know that malloc() allocates memory only in powers of 2. i.e if asked for say 17 bytes , in the process, it allocates 32 bytes and returns...
42
2128
by: Joris Adriaenssens | last post by:
This is my first posting, please excuse me if it is off-topic. I'm learning to program in C. It's been almost ten years I've been programming and a lot of things have changed apparently. I...
19
26676
by: Jerry | last post by:
I am wondering what is the maximum size of memory that malloc() could handle. Is there any limitation on that? Where am I supposed to get this kind of information? Thank you everybody.
29
4354
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
17
1691
by: Christopher Benson-Manica | last post by:
Some recent posts got me thinking about how one might have dealt with simplistic malloc() implementations which might return NULL for a 64K request but might accept two 32K requests or four 16K...
6
3353
by: itsolution | last post by:
Hi folks, Could you shed some light on this issue? my program is running on Freebsd as a daemon. When user sends a request, it forks itself and lets its child process handles the request....
11
1650
by: pushpakulkar | last post by:
Hi all, Many users claim that malloc() and free() provided by the language library results in considerable amount of overhead in terms of calling the same. Can we really eliminate overhead by...
0
7202
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
7280
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,...
1
6991
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
7462
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
5014
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
4673
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
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
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.