aspose file tools*
The moose likes Java in General and the fly likes how to clear the stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to clear the stack" Watch "how to clear the stack" New topic
Author

how to clear the stack

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I am running a recursive program, and at one point in time, the stack overflows.
I am trying to find if it is possible to clear the stack once it overflows
If possible how to do that??? and is there any side effects of doing so??


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
sambasiva kumar
Greenhorn

Joined: Jan 16, 2007
Posts: 24
Once the stack overflows you are already in the StackOverflow Exception.............I really wonder how you will clear the stack after the program is aborted.
Check out for an alternate solution such that you will not reach the stack overflow.
Keep the count of number of records before the stack is full and then clear the same before the count is reached.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

sambasiva kumar wrote:Once the stack overflows you are already in the StackOverflow Exception.............I really wonder how you will clear the stack after the program is aborted.

I am using try/catch , and i though i will clear the stack(if possible) in the catch block



sambasiva kumar wrote:
Keep the count of number of records before the stack is full and then clear the same before the count is reached.

you said to clear , clear from where??? stack ???
sambasiva kumar
Greenhorn

Joined: Jan 16, 2007
Posts: 24
Clear the stack in the try block itself before it even goes into the catch.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

sambasiva kumar wrote:Clear the stack in the try block itself before it even goes into the catch.

So back to the same question ??? how do i clear the stack??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42945
    
  68
The only way to clear up memory in this situation is reduce the number of stack frames. In other words, you need to return from a number of the nested method calls.

Keep the count of number of records before the stack is full

The problem with that is that this number may not be the same in each run of the code. Sometime it may be reached after 100000 recursive calls, in other circumstances after 115000. It depends on what the method does and how much memory it uses.
sambasiva kumar
Greenhorn

Joined: Jan 16, 2007
Posts: 24
use the pop() method in the Stack to clear the stack(pop till there are no more objects in the stack) and then continue with your program.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Ulf Dittmer wrote:The only way to clear up memory in this situation is reduce the number of stack frames. In other words, you need to return from a number of the nested method calls.


Can you explain it a bit more..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42945
    
  68
James Tharakan wrote:
Ulf Dittmer wrote:The only way to clear up memory in this situation is reduce the number of stack frames. In other words, you need to return from a number of the nested method calls.
Can you explain it a bit more..

Each recursive call occupies additional memory on the stack. If the code made 100000 calls and the stack then overflows, you'd need to return from a fair number of those calls in order to free up some of that memory: http://en.wikipedia.org/wiki/Call_stack

What kind of algorithm is this that you need to recurse so deeply? How many objects does the method allocate each time it's invoked - can you reduce that number?

use the pop() method in the Stack to clear the stack(pop till there are no more objects in the stack) and then continue with your program.

We're talking about the JVM's internal stack, not about the Stack class.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I am try to do the following.Iam not sure if that is the right way.
Ex: If i have the number 1234, then i am generate the numbers 1233,1232,1231,1230,1229,.....till 1111.
So i am calling the method passing the number 1234, so the recursive call would be by the number 1233 and so on...
Not sure if this method is effective.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

So i am calling the method passing the number 1234, so the recursive call would be by the number 1233 and so on...

Think of a way to do it without recursion A for loop should do the job.


[My Blog]
All roads lead to JavaRanch
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Christophe Verré wrote:
So i am calling the method passing the number 1234, so the recursive call would be by the number 1233 and so on...

Think of a way to do it without recursion A for loop should do the job.

The reason why i choose recursion is because the length of the number is not know... i.e if it is 5 digit or 6 digit or ...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42945
    
  68
Why is the length of the number important? The algorithm simply counts down by 1 until the number consists only of "1" digits, doesn't it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

One rule from computer science is that recursive algorithms can always be transformed into non-recursive ones, by using a loop instead of recursion. So it is certainly be possible to change your program to use a loop instead of recursion.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Ulf Dittmer wrote:Why is the length of the number important? The algorithm simply counts down by 1 until the number consists only of "1" digits, doesn't it?

Sorry for misleading...
it should be like..
1234
1233
1232
1231
1230

1224
1223
1222
1221
1220

1214
1213
1212
1211
1210

1204
1203
1202
1201
1200

.....
....


Its like the , getting all combinations
00
01
10
11


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

One of the hardest things in programming is defining the problem you are trying to solve.

I, personally, still don't understand what you are trying to do. What would you expect to get if the initial input is

423?

89?

0?

34279834292389047?

unless or until you can define EXACTLY what the program is supposed to do, how can you try to write the program?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

James Tharakan wrote:it should be like..
1234
1233
1232
1231
1230

1224
1223
1222
1221
1220

1214
1213
1212
1211
1210

1204
1203
1202
1201
1200

.....
....


Its like the , getting all combinations
00
01
10
11




That example looks to me like you're given a number in base 5 and you're counting down in base 5.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

That example looks to me like you're given a number in base 5 and you're counting down in base 5.

Which can still be done by decrementing a variable in a for loop.
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
First of all, stack overflow is an error, not an exception, so you can't try catch it.

And as mentioned above your problem (if it was identified correctly above, again, be clear as to what problem you're trying to solve) doesn't require recursion nor is recursion a best fit. For example, why not simply count (down?) in base X where X is the number of original digits. Also, even with recursion this problem shouldn't cause stack overflows since the stack size should not be significantly higher than the number of digits X.
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
Oh and please post the relevant bit of code ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
R van Vliet wrote: . . . stack overflow is an error, not an exception, so you can't try catch it. . . .
Yes, you can. But you probably can't do anything useful about it, so you ought not to attempt it.

I presume this is an exercise in writing recursive methods, so recursion would be "required reading."
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
Campbell Ritchie wrote:
R van Vliet wrote: . . . stack overflow is an error, not an exception, so you can't try catch it. . . .
Yes, you can. But you probably can't do anything useful about it, so you ought not to attempt it.

I presume this is an exercise in writing recursive methods, so recursion would be "required reading."


Really? How does one catch it then (regardless of it's usefulness). The JDK only has a StackOverflowError, and not a StackOverflowException of some sort. I'm also quite sure all stack overflow problems result in a java.lang.StackOverflowError.
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
Can you confirm that you require the solution to the following problem :

INPUT : A number of unique digits
OUTPUT : All integers composed of all or a subset of the INPUT digits

So input 145 would give 1, 4, 5, 11, 14, 15, 41, 44, 45, 51, 54, 55, 111, 114, 115, 141, 144, 145, etc.

Correct?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Its a bit hard to explain now to tell WHY i am trying to generate these number.But i will explain what i am trying to do.
SUPPOSE the number of digits we have is 4 and the number is 5534 (the number of digits and the actual number is not know till runtime.)

Step 1: Now the number in the units place should be counted down to one, so we will get four different numbers, i.e 5534,5533,5532,5531.
step 2: Now the number in the ten's place should be counted down by one and repeat the step 1, so again we get 4 number, i.e 5524,5523,5522,5521
Step 3: Now the number in the ten's place should be counted down by one and repeat the step 1, so again we get 4 number, i.e 5514,5513,5512,5511

and so move to hundred's place and then to thousands places....

Right now i am able to generate(i guess) the numbers, but because of stack over flow i able not able to complete run the program.
One work around is to save the point where it broke in a file and then continue in the next run.


Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
If you're getting a stack overflow I'm guessing you have a bug. If you don't think you do, then increase the amount of memory for the application. (I guess you'd use XssStackSize parameter -- not sure though.) Even if it were possible, you don't want to pop or clear the stack, since your recursion depends on it. As you unwind the recursion, those program states are needed.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Max Rahder wrote:Even if it were possible, you don't want to pop or clear the stack, since your recursion depends on it. As you unwind the recursion, those program states are needed.


I think ,since i am storing the actual array and the manipulated array at every point, clearing the stack would not cause a problem
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
Deleted, misunderstood your requirement ;)
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144


Is still extremely dodgy code. Errors should not be caught. Only the JVM itself can recover from them and there is no guarantee that it will (granted, StackOverflowError is sort of an exception to this rule since afaik tje JVM always recovers from it, how else can it show the stack trace).

Anyway, your code is bugged, and clearing the stack and such is not a solution to your problem. The recursive solution requires at most X+1 stack frames where X is the number of digits. Unless you're working with enormous numbers you cannot have a StackOverflowError. If I have some time later I'll check your code. Is recursion an actual requirement (the problem is easier with a 3 level loop, no recursion)?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
According to Bruce Eckel in Thinking in Java 4th edition], the JVM does not recover from an uncaught Error or RuntimeException. What happens is that the Exception propagates to the JVM itself, which prints a stack trace and then terminates the affected Thread.

You can catch an Error like thisFor ThreadDeath, read its API documentation.

So you can catch an Error, but as we have seen there is usually nothing useful you can do with that Error, so it might as well be allowed to propagate.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Christophe Verré wrote:
That example looks to me like you're given a number in base 5 and you're counting down in base 5.

Which can still be done by decrementing a variable in a for loop.


Yes, exactly. No recursion required to count down, that was the point I meant to make.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Paul Clapham wrote:
Christophe Verré wrote:
That example looks to me like you're given a number in base 5 and you're counting down in base 5.

Which can still be done by decrementing a variable in a for loop.


Yes, exactly. No recursion required to count down, that was the point I meant to make.

what is this idea of base
can you explain???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I presume base 5 is like saying we are used to counting in base 10: 1-2-3-4-5-6-7-8-9-10. In base 5 you would count 1-2-3-4-10-11-12-13-14-20, so 100base5 would be the same as 25base10.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to clear the stack