printing a file in reverse order is easy in recursion rather than in iteration

Hi,

I am reading as

printing a file in reverse order is easy in recursion rather than in iteration

I was not clear on what it means. please advise.
LVL 7
gudii9Asked:
Who is Participating?
 
dpearsonConnect With a Mentor Commented:
It's not good advice.  If you want to print out a file in reverse order, iterative solutions are better.

The reason is say you have a 100MB file.

To read it into a buffer and then walk back up through the buffer printing it out (iterative solution), you will use 100MB on the heap.
To read it in a byte at a time in a recursive solution (and then print it out in reverse order) you will use more than 100MB on the stack (since recursive functions are stored on the stack).  That will crash your Java process.  Heap space is always a lot larger than stack space.

Doug
0
 
gudii9Author Commented:
 Heap space is always a lot larger than stack space.

how much is usual space of heap and stack? please advise
0
 
dpearsonCommented:
how much is usual space of heap and stack? please advise

You can configure these settings, but heap on a "normal" computer today might be perhaps 2GB while stack space might be 1MB.

So a difference of 2000 times.

Doug
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.