Strange intermittent error

Hi everyone!

I'm running a Physics simulation. I enter a number of particles to simulate, and a number of time steps to run it through. It's working completely fine for smaller numbers, but when I run it with a lot of particles through a lot of time steps, I'm getting this error:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mayer:experimental3d declan$ ./rundsmc3d 
Enter number of simulation particles: 50000
Each particle represents 67125 atoms
System width is 79.899 mean free paths
Enter total number of time steps: 500
Done 100 of 500 steps; 6 collisions
Total wall strikes: 0 (left) 0 (right)
Done 200 of 500 steps; 55 collisions
Total wall strikes: 0 (left) 0 (right)
Done 300 of 500 steps; 315 collisions
Total wall strikes: 0 (left) 0 (right)
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
terminate called after throwing an instance of 'std::bad_alloc'
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
rundsmc3d(35407,0xa00cb720) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Abort trap 


Sorry for all the repeats, but I wanted to show the end. I'm guessing this is because of me making arrays too big or something, or the stack running out of memory? Can anyone shed some light on this?

Thanks!

Edit: Wait, I checked. I wrote my class that holds most of the data this program is moving around (called Matrix3d) with an array that is initialized this way:

data_ = new double[ncell_x*ncell_y*ncell_z];

This is off the heap, isn't it? And I thought that was safer/had more memory available than the stack. Can I still use all the heap memory somehow?
Last edited on
Can I still use all the heap memory somehow?
Unless you have a system with infinite memory, yes.

Send to standard output the values of those variables before allocating the array. There may be unassigned variables at play, or perhaps a memory leak, or you just may have ran out or memory.
The fact that the allocation fails only after some iterations strongly suggests that there's a memory leak (assuming each iteration doesn't need to keep the information generated by the last iteration for too long).
Ok, this problem is coming up again. How can I pinpoint where in my program the memory leak might be?

Are the only causes of memory leaks not deleting things created off the heap when I'm done with them?

Thanks!
Make sure that when ever you call new to create an object you delete it once it's no longer need. Make sure that before you reassign a pointer to a new object you delete the object it was pointing to, if it's no longer needed. If you are using external library functions make sure that if you call a function that creates and object, you later call a function to delete once it's of no use. If I were to take a wild guess, i'd think that you were probably creating a new object in a loop and forgetting to delete it.
Got it! First, I had a loop, with

1
2
3
4
double * stats1 = new double[];
double * stats2 = new double[];
double * stats3 = new double[];
double * stats = new double[];


Without deleting it. Then I added a delete for each of them. Then I realized that the way I was initializing them to begin with was stupid, since I had it do:

 
stats1 = wallBounce();


where wallBounce() returns a pointer to an array of doubles of length 5. So I made it initialize them with:

1
2
3
4
double * stats1 = NULL;
double * stats2 = NULL;
double * stats3 = NULL;
double * stats = NULL;


But there was a further, more subtle bug. At a few points, it might do something like this:

1
2
3
4
5
6
stats1 = wallBounce();
if(something)
     stats = stats1;

delete [] stats;
delete [] stats1;


which gave an error about deleting the same block of memory twice. So I fixed it with:

1
2
3
4
5
6
7
stats1 = wallBounce();
if(something){
     stats = stats1;
     stats1 = NULL;
}
delete [] stats;
delete [] stats1;


Problem solved! Thanks guys!
Topic archived. No new replies allowed.