Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack growing too big  RSS feed

 
Gillian Bladen-Clark
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a parser that checks for regular expressions in text files. I set up a variety of strings to define what I'm looking for then call a
method that compiles the string into a pattern and tells me whether the current line matches the pattern.
For efficiency, I defined these strings as statics.
This all works fine but when I process a very large file, I find that it
eventually grinds to a halt. m.matches calls java.util.regex.match()
which is recursive so I expect to a few occurrences of match() in the
stack. However, if I watch the call stack, I see that the number of calls
to java.util.regex.match() grows bigger and bigger as the program
progresses for the same value of toFind i.e. the same pattern.
To reproduce this problem, here is a cut-down version.

Just run it against a text file containing say 300 lines of text e.g.

etc etc
Put a breakpoint at this line

in the java.util.regex.pattern class - public static final class GroupHead extends Node
(This is line 3900 in my source dated 1.87 02/07/10).
When the breakpoint is reached, look at the stack then continue.
The stack grows each time this breakpoint is reached.
Any ideas why the stack is growing so big ?
[ January 05, 2004: Message edited by: Gillian Bladen-Clark ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the code on a file containing 300 lines of "one two" without trouble. Running it will a debugger and breakpoint shows the stack shrinking and growing, but never getting much higher than about 20 levels. I'm using the JSDK 1.4.2. My only change was to replace the TextReader with a BufferedReader -- presumably they behave the same way.
It's odd that you had a different result. I wouldn't expect a typical regex engine to need much in the way of recursion, so I'm surprised that you ran into this problem.
 
Gillian Bladen-Clark
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for taking the time and trouble to try it out. It's a strange one for sure - I'm using the same SDK so I don't really understand what's causing me the problem.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that I have noticed is that you compile your pattern inside your "isMatch" method so it is done again every time round the loop. Compiling a pattern is a relatively expensive operation, so you should probably move that outside the method and pass in a compiled pattern rather than a "tofind" string.
Other than that I'm also stumped. I've looked at this several times now, and still can't see anything strange about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!