Jump to content

Find limit of recursion: Difference between revisions

Go solution
No edit summary
(Go solution)
Line 421:
# No limit (may crash GAP if recursion is not controlled) :
SetRecursionTrapInterval(0);</lang>
=={{header|Go}}==
Go features stacks that grow as needed, so I expected a rather large recursion limit. I used this simple program, that prints every 1000 levels:
<lang go>package main
 
import "fmt"
 
func main() {
r(1)
}
 
func r(l int) {
if l % 1000 == 0 {
fmt.Println(l)
}
r(l+1)
}</lang>
I tested on a smallish computer by today's standards, 1 GB RAM, and the standard Ubuntu installation gave it 2.5 GB swap. The program filled available RAM quickly, at a recursion depth of about 10M. It took a several minutes then to exhaust swap before exiting with this trace: (as you see, at a depth of over 25M.)
<pre>
...
25611000
25612000
25613000
25614000
throw: out of memory (FixAlloc)
 
runtime.throw+0x43 /home/sonia/go/src/pkg/runtime/runtime.c:102
runtime.throw(0x80e80c8, 0x1)
runtime.FixAlloc_Alloc+0x76 /home/sonia/go/src/pkg/runtime/mfixalloc.c:43
runtime.FixAlloc_Alloc(0x80eb558, 0x2f)
runtime.stackalloc+0xfb /home/sonia/go/src/pkg/runtime/malloc.c:326
runtime.stackalloc(0x1000, 0x8048c44)
runtime.newstack+0x140 /home/sonia/go/src/pkg/runtime/proc.c:768
runtime.newstack()
runtime.morestack+0x4f /home/sonia/go/src/pkg/runtime/386/asm.s:220
runtime.morestack()
----- morestack called from goroutine 1 -----
main.r+0x1a /home/sonia/t.go:9
main.r(0x186d801, 0x0)
main.r+0x95 /home/sonia/t.go:13
main.r(0x186d800, 0x0)
main.r+0x95 /home/sonia/t.go:13
main.r(0x186d7ff, 0x0)
main.r+0x95 /home/sonia/t.go:13
main.r(0x186d7fe, 0x0)
main.r+0x95 /home/sonia/t.go:13
main.r(0x186d7fd, 0x0)
 
... (more of the same stack trace omitted)
 
 
----- goroutine created by -----
_rt0_386+0xc1 /home/sonia/go/src/pkg/runtime/386/asm.s:80
 
goroutine 1 [2]:
runtime.entersyscall+0x6f /home/sonia/go/src/pkg/runtime/proc.c:639
runtime.entersyscall()
syscall.Syscall+0x53 /home/sonia/go/src/pkg/syscall/asm_linux_386.s:33
syscall.Syscall()
syscall.Write+0x5c /home/sonia/go/src/pkg/syscall/zsyscall_linux_386.go:734
syscall.Write(0x1, 0x977e4f18, 0x9, 0x40, 0x9, ...)
os.*File·write+0x39 /home/sonia/go/src/pkg/os/file_unix.go:115
os.*File·write(0x0, 0x0, 0x9, 0x40, 0x9, ...)
os.*File·Write+0x98 /home/sonia/go/src/pkg/os/file.go:141
os.*File·Write(0xbffe1980, 0x8, 0x9, 0x8048cbf, 0x186d6b4, ...)
----- goroutine created by -----
_rt0_386+0xc1 /home/sonia/go/src/pkg/runtime/386/asm.s:80
</pre>
Hey, at least it terminated in a controlled way. I tried this task a few months ago and it crashed the whole computer. I read that the Go runtime has since been improved to handle out of memory conditions more gracefully. Seems so&mdash;my machine is still up.
 
=={{header|Groovy}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.