realloc() fails


 
Thread Tools Search this Thread
Top Forums Programming realloc() fails
# 8  
Old 08-16-2011
Agreed... Though at 60% utilization you are also getting worryingly close to 32-bit address size limits. The heap can't use all 4 gigs because there's other things in the way.
# 9  
Old 08-16-2011
Quote:
Originally Posted by jlliagre
You still don't give a clue about the allocated area sizes
Ok, more accurately but still quite roughly:
at the starting of the C++ program the OS is using ~0.3GB or ram. The program stores ~3000 text files of unknown size in ram, one by one, via realloc(). All the files should fit into less than 2GB and, anyway, when realloc() fails there are still ~0.8GB available (40% of 2GB). At that time realloc() has allocated 2-0.3-0.8 ~=0.9GB.
Each file is stored independently: realloc() makes space for one single file at each time. The space is allocated in increment of 500KB. It means that if a file is 800KB, realloc() works twice and allocates 1MB.

It happens that when the allocated memory reaches ~1.2GB (OS=0.3, program=0.9), realloc() returns NULL when attempting to allocated another 500KB.

As long as realloc() does not allocated more than those ~0.9GB, everything works just fine.

Thanks again.
# 10  
Old 08-16-2011
You are focusing too much on RAM. realloc() doesn't care or even use RAM. What this function does is reserving (or releasing) virtual memory. Whether this virtual memory is backed by RAM or not doesn't matter.

You should monitor your process virtual memory usage and see if you don't exhaust it. You might also see how scattered your process virtual memory is with using "pmap -d pid".

And by the way, why aren't you directly allocating the file size ? or even better, why aren't you using mmap to directly load the files in memory ?
This User Gave Thanks to jlliagre For This Post:
# 11  
Old 08-16-2011
Thanks! Will do and eventually will post again.

---------- Post updated at 04:52 PM ---------- Previous update was at 04:34 PM ----------

Quote:
Originally Posted by jlliagre
And by the way, why aren't you directly allocating the file size ? or even better, why aren't you using mmap to directly load the files in memory ?
Because I do NOT know the size of the files. They are coming from the Internet. Instead of storing them first on HD and then loading them in memory, it's *much* faster receiving them directly in memory.

---------- Post updated at 06:25 PM ---------- Previous update was at 04:52 PM ----------

Ok, while I'm digging into something that I never faced (memory management), I'd like to take a short cut and ask you guys how I should interpret the output of vmstat and pmap.

In quiet mode, before launching the infamous C++ program here is the output of vmstat:
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 900908 64892 727084 0 0 7 14 154 62 4 1 95 0


Then I launch the program.

In the middle of its exec time `pmap -x PID | grep anon` gives (after grep'ing out spaces smaller than 1000):
59e17000 5904 - - - rw--- [ anon ]
5a3db000 1468 - - - rw--- [ anon ]
5a54b000 1468 - - - rw--- [ anon ]
5a6bb000 63468 - - - rw--- [ anon ]
5e6a0000 24108 - - - rw--- [ anon ]
60015000 6396 - - - rw--- [ anon ]
6083e000 189420 - - - rw--- [ anon ]
6c323000 165804 - - - rw--- [ anon ]
766f8000 39852 - - - rw--- [ anon ]
78de3000 1468 - - - rw--- [ anon ]
78f52000 16236 - - - rw--- [ anon ]
7a117000 119064 - - - rw--- [ anon ]
81652000 122016 - - - rw--- [ anon ]
88e6f000 34932 - - - rw--- [ anon ]
8b3e6000 3444 - - - rw--- [ anon ]
8b92d000 202704 - - - rw--- [ anon ]
97f9b000 94956 - - - rw--- [ anon ]
9dfb0000 69864 - - - rw--- [ anon ]
a25d4000 135300 - - - rw--- [ anon ]
aaaea000 114636 - - - rw--- [ anon ]
b1add000 1468 - - - rw--- [ anon ]
b1c4d000 1468 - - - rw--- [ anon ]
b1dbd000 91788 - - - rw--- [ anon ]
total kB 1541848


Just before realloc() fails here is again
`pmap -x PID | grep anon` (after grep'ing out spaces smaller than 1000):

00adc000 1476 - - - rw--- [ anon ]
00ee4000 116112 - - - rw--- [ anon ]
080c9000 25708 - - - rw--- [ anon ]
099f1000 198768 - - - rw--- [ anon ]
15c0d000 1468 - - - rw--- [ anon ]
15d7d000 1468 - - - rw--- [ anon ]
15eed000 91020 - - - rw--- [ anon ]
1b846000 352764 - - - rw--- [ anon ]
310c5000 1956 - - - rw--- [ anon ]
312ae000 1468 - - - rw--- [ anon ]
3141e000 61992 - - - rw--- [ anon ]
35292000 5412 - - - rw--- [ anon ]
358d0000 31488 - - - rw--- [ anon ]
3797a000 49692 - - - rw--- [ anon ]
3aa78000 137268 - - - rw--- [ anon ]
4326f000 7872 - - - rw--- [ anon ]
43c09000 28536 - - - rw--- [ anon ]
459d1000 20172 - - - rw--- [ anon ]
46e79000 39360 - - - rw--- [ anon ]
495de000 34440 - - - rw--- [ anon ]
4b96a000 7872 - - - rw--- [ anon ]
4c304000 10824 - - - rw--- [ anon ]
4cf80000 208116 - - - rw--- [ anon ]
59b35000 8856 - - - rw--- [ anon ]
5a3db000 1468 - - - rw--- [ anon ]
5a54b000 1468 - - - rw--- [ anon ]
5a6bb000 63468 - - - rw--- [ anon ]
5e6a0000 24108 - - - rw--- [ anon ]
60015000 6396 - - - rw--- [ anon ]
6083e000 189420 - - - rw--- [ anon ]
6c323000 165804 - - - rw--- [ anon ]
766f8000 39852 - - - rw--- [ anon ]
78e5c000 17220 - - - rw--- [ anon ]
7a117000 119064 - - - rw--- [ anon ]
81652000 122016 - - - rw--- [ anon ]
88e6f000 34932 - - - rw--- [ anon ]
8b3e6000 3444 - - - rw--- [ anon ]
8b92d000 202704 - - - rw--- [ anon ]
97f9b000 94956 - - - rw--- [ anon ]
9dfb0000 69864 - - - rw--- [ anon ]
a25d4000 135300 - - - rw--- [ anon ]
aaaea000 114636 - - - rw--- [ anon ]
b1add000 1468 - - - rw--- [ anon ]
b1c4d000 1468 - - - rw--- [ anon ]
b1dbd000 91788 - - - rw--- [ anon ]
b7772000 65936 - - - rw--- [ anon ]
bb7d6000 3420 - - - rw--- [ anon ]
bbb2d000 43296 - - - rw--- [ anon ]
total kB 3112744 - - -


How should I interpret these info?


---------- Post updated at 06:29 PM ---------- Previous update was at 06:25 PM ----------

Quote:
Originally Posted by mamboknave
Ok, while I'm digging into something that I never faced (memory management), I'd like to take a short cut and ask you guys how I should interpret the output of vmstat and pmap.
Mistakenly, I posted some further info as a reply to myself. Can you guys pls take a look at my last post?

Thanks!
# 12  
Old 08-17-2011
Quote:
Originally Posted by mamboknave
Instead of storing them first on HD and then loading them in memory, it's *much* faster receiving them directly in memory.
Why would that be? All memory is swap backed technically, this is just disk-backed instead. If you save it on disk it'll be in cache, and cache is memory too. It gets paged in on-demand just like any other memory, with the advantage of not wasting swap space. It's quite efficient actually.

---------- Post updated at 12:08 AM ---------- Previous update was at 12:06 AM ----------

Quote:
Originally Posted by mamboknave
How should I interpret these info?
That you've used up 3 gigabytes of address space if not memory, way beyond the limit I'd have suspected your program would begin to bug out on. In any case the reason for the errors is clear. You're running out of address space.

mmap() might be able to fill in the corners you're not using, but there's not a lot left to be had.
# 13  
Old 08-17-2011
Yes. Either buy some ram and move to 64 bit or fix your application design. The OS is better suited to store thousands of files than a user application.
If you feel 3+ GB of memory isn't what your application should use, maybe is there a memory leak in it. You might use valgrind to investigate that.
# 14  
Old 08-17-2011
When you're reading from a network, writing to disk is going to be much, much faster than your network speeds unless your disks are stone tablets with data being carved into them by gnomes.

And what do you plan on doing WHEN you run into a bug and your program crashes in production with all these files in RAM? Did you even think of that?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

problem with realloc( i think is gcc :/ )

Hi everyone, i made this program. is a simple one for practising malloc, realloc and structs. I have a struct named shop as global variable in which i take the size of the matrix from the keyboard and after i malloc it. I insert the values with the fullarray() and after i print the matrix with... (7 Replies)
Discussion started by: giampoul
7 Replies

2. UNIX for Dummies Questions & Answers

about realloc routing

#include <malloc.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int* allocat_array(void) { int *array; int tmp; int n_values = 0 ; array = malloc(sizeof(int)); if(array == NULL) return NULL; while(scanf("%d",&tmp) != EOF) { ... (1 Reply)
Discussion started by: vincent__tse
1 Replies

3. Programming

Realloc() question

b = realloc(a, 1000); if realloc succeeds and b!=a (not in-place replacement), does realloc automatically free a or I should free both a and b afterwards? thank you! (5 Replies)
Discussion started by: bashuser2
5 Replies

4. Programming

realloc fails in C : what next ?

Hi all I'm trying to use someone else's software, which has a realloc that fails in it. This is probably due to memory limitations, as it only happens when I use this software on huge datasets. First question : how to diagnose if it's a soft or hard limitation? I mean, if it's due to my... (10 Replies)
Discussion started by: jossojjos
10 Replies

5. Programming

What happens when realloc() fails?

Hi, I am seeing varying results about, when realloc() fails in reallocation. Which one is correct out of the below? a) realloc() maintains the original pointer (i.e) the original pointer is left unaltered/untouched but relloc() returns the NULL value. b) original buffer pointer is lost... (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. Programming

malloc vs realloc

Why when using realloc, john is reversed 3 times but not the other 2 names ? But if I use malloc, then the 3 names are reversed correctly ? (but then there is a memory leak) How can I reverse all 3 names without a memory leak ? char *BUFFER = NULL; char *STRREVERSE(const char *STRING) {... (5 Replies)
Discussion started by: cyler
5 Replies

7. Programming

tolower (static pointer + malloc + realloc)

N00B here. This function would be easier using a char pointer along with free. But I wish to learn how to use char static pointers (they do not require free, right ?). How do I erase the content of a static pointer ? Terminating the string works but the static pointer's content is not being... (4 Replies)
Discussion started by: limmer
4 Replies

8. Programming

help with realloc() on Linux

hi, I'm using gcc version 3.4.6 on a Red Hat system... (not sure how to determine version of glibc) when i run the following, i get: glibc detected *** realloc(): invalid next size: 0x0804a170 I'm not sure what is wrong. The error happens on the second iteration of the while loop.... (3 Replies)
Discussion started by: Andrewkl
3 Replies

9. Programming

Does realloc free fairly?

Hello, my program works properly but valgrind tells me I am not freeing allocated memory. I think the problem is in realloc. I am pretty sure I do something wrong with realloc, because I changed it a bit and valgrind noticed less errors (that the program wasn't working properly with less errors... (3 Replies)
Discussion started by: samciz
3 Replies

10. Programming

Realloc

Can Any body give me a exampla which has the usage of realloc i want a function which uses realloc & increases /decreases the size of a pointer (0 Replies)
Discussion started by: wojtyla
0 Replies
Login or Register to Ask a Question