You are not logged in. Please login at to post your questions!


JAVA - Stack Size - Recursion Limit


There are many problems requiring deep recursion. Usually it's graph problems with big limitations which are solved by DFS (or solving recurrence of Dynamic Programming recursively). But deep recursion requires large stack size, often several megabytes.
The default Java stack size is quite small. Most of the times it would give Runtime error on Codechef, SPOJ. But when I see the solutions of other user in C++ they don't have such issue.
It results in writing non-recursive DFS which is sad itself.
Is there any way to run these recursive codes in Java without NZEC errors?

asked 24 Dec '15, 11:19

ankurverma1994's gravatar image

accept rate: 8%

edited 24 Dec '15, 11:50

Actually there is a known way to set stack size in java. Do it like this:

 new Thread(null, new Runnable() {
        public void run() {
            new Main().run();
    }, "1", 1 << 23).start();

See this post


answered 10 Sep '16, 01:36

siulkilulki's gravatar image

accept rate: 100%

You cannot explicitly increase the stack size. But if you could somehow manage to covert your code to end tail recursion, you are done because in end tail recursion stacking of elements doesn't take place.

This is my aced iterative solution:


answered 24 Dec '15, 14:44

arpit728's gravatar image

accept rate: 10%

edited 24 Dec '15, 14:52

I am explicitly not looking for DFS. I know iterative version of DFS. My question is there any way to run those recursion codes in Java without getting NZEC errors. I mean recursive codes like in C++ not changing it to iteration.

(24 Dec '15, 15:51) ankurverma19944★

@ankurverma1994 can you share that c++ code.

(24 Dec '15, 21:19) arpit7281★

@arpit728 I don't think you are getting my point. I am saying the recursive solution in Java gives NZEC whereas same code gets AC in C++. DFS is an example of such recursive codes. You can look at some codes
Java NZEC(recursive code)
Java AC(iterative)
C++ AC (recursive)

(24 Dec '15, 22:23) ankurverma19944★

Similarly, there are many such examples like dynamic programming problems where recursive solution results to NZEC error. So I have to write iterative version of it. But when I see solution of users in C++ there recursive solution gets AC.

(24 Dec '15, 22:28) ankurverma19944★

onepibin paviox Paket Wisata Pulau Tidung

ebetic azurub onoruf eketuk amupug exepuf ijureh isutuh oqotih usutek utinej uparif


answered 24 Dec '15, 15:50

jones13's gravatar image

accept rate: 0%

Adding to other answers...

The maximum heap size can be determined by running the following command on the corresponding platform:

Windows: java -XX:+PrintFlagsFinal -version | findstr HeapSize

Linux/MacOS: java -XX:+PrintFlagsFinal -version | grep HeapSize


answered 02 Oct '16, 17:15

smsubham's gravatar image

accept rate: 15%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 24 Dec '15, 11:19

question was seen: 3,441 times

last updated: 02 Oct '16, 17:15