• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 114
  • Last Modified:

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.
0
gudii9
Asked:
gudii9
  • 2
1 Solution
 
dpearsonCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now