Java Stack Overflow Error Example
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack stack overflow error java Overflow the company Business Learn more about hiring developers or posting ads with us how to solve stack overflow error in java Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community
How To Resolve Stack Overflow Error In Java
of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What is a StackOverflowError? up vote 221 down vote favorite 74 What is a StackOverflowError, what
Java Increase Stack Size
causes it, and how should I deal with them? java exception-handling stack-overflow share|improve this question edited Jun 19 '15 at 4:55 asked Oct 18 '08 at 8:13 Ziggy 7,384165475 add a comment| 11 Answers 11 active oldest votes up vote 230 down vote accepted Parameters and local variables are allocated on the stack (with reference types the object lives on the heap and a variable references that object). The stack typically lives java stackoverflowerror recursion at the upper end of your address space and as it is used up it heads towards the bottom of the address space (i.e. towards zero). Your process also has a heap, which lives at the bottom end of your process. As you allocate memory this heap can grow towards the upper end of your address space. As you can see, there is the potential for the heap to "collide" with the stack (a bit like tectonic plates!!!). The common cause for a stack overflow is a bad recursive call. Typically this is caused when your recursive functions doesn't have the correct termination condition, so it ends up calling itself forever. However, with GUI programming it's possible to generate indirect recursion. For example, your app may be handling paint messages and whilst processing them it may call a function that causes the system to send another paint message. Here you've not explicitly called yourself, but the OS/VM has done it for you. To deal with them you'll need to examine your code. If you've got functions that call themselves then check that you've got a terminating condition. If you have then check than when calling the function you have at least modified one of the arguments, otherwise there'll be no visible
and DropEventsExpandableListViewGridViewHorizontalScrollViewImageButtonImageViewLinearLayoutListViewNotificationsPasswordProgressBarProgressDialogRadioButtonRadioGroupRatingBarRelativeLayoutScrollViewSelectorSettingsSpinnerSurfaceViewTextBoxTimePickerToastToggleButtonWebViewviewMenuOnClickListenerViewPagerwidgetAutoCompleteTextViewEditTextFrameLayoutSeekBarSlidingDrawerStackViewTextViewViewFlipperxmlgamescanvasmain loopOpenGL ESCore JavaanimationapacheANTcommonsbeanutilsconvertersArrayConvertercliBasicParsercodecbinaryBase64Base64OutputStreamcsvdbcpBasicDatasourcePoolingConnectioniocomparatorCompositeFileComparatorDirectoryFileComparatorLastModifiedFileComparatorNameFileComparatorPathFileComparatorFilenameUtilsFileUtilsIOUtilsmonitorFileAlterationMonitorlang3CharUtilsClassPathUtilsmathFractionNumberUtilsStringUtilsloggingLogfactorynetCookieStoreFTPClientURLClassLoaderURLConnectionluceneappletaspectjbeanscajoCharacterclassComperablecryptoDesign PatternsdecoratorstrategyGenericsGradlegsonGsonBuilderstreamJsonReaderJsonWriterioBufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriterByteArrayInputStreamByteArrayOutputStreamConsoleDataInputStreamDataOutputStreamExternalizableFileFileDescriptorFileInputStreamFilenameFilterFileOutputStreamFileReaderFileWriterInputStreamInputStreamReaderIOExceptionObjectInputStreamObjectOutputStreamOutputStreamPrintWriterRandomAccessFileSerializabl
puts automation in the app First look: Google Cloud Machine Learning soars 7 habits of highly effective developers 10 hard-core coding tips for http://www.javaworld.com/community/node/3153 faster Python More Insider Sign Out Search for Suggestions for you Insider http://www.herongyang.com/JVM/Stack-Overflow-StackOverflowError-Exception-Test.html email Core Java All Core Java Agile Development Java Concurrency Java Language Java Platform Java Security Programming Careers Testing and Debugging Enterprise Java All Enterprise Java Big Data Cloud Computing Data Analytics Development Tools Java APIs Java App Dev Java Web Development Open Source Scripting and JVM stack overflow Languages Learn Java All Learn Java Design Patterns Mobile Java All Mobile Java Java Android Developers Java iOS Developers News Features How-Tos Blogs Resources/White Papers Newsletters × Close Home Dustin's Software Development Cogitations and Speculations By Dustin Marx About | A software developer's public collection of tips and tricks, real-world solutions, and industry commentary related to Java programming. stack overflow error How-To Diagnosing and Resolving StackOverflowError More like this Effective Java NullPointerException Handling JavaScript in Java Writing good unit tests, Part 2: Follow your nose Email a friend To Use commas to separate multiple email addresses From Privacy Policy Thank you Your message has been sent. Sorry There was an error emailing this page. Comments JavaWorld | Jul 4, 2009 3:04 PM PT Comments A recent JavaWorld Community forum message (Stack Overflow after instantiating new object) reminded me that the basics of the StackOverflowError are not always understood well by people new to Java. Fortunately, the StackOverflowError is one of the easier of the runtime errors to debug and in this blog posting I will demonstrate how easy it often is to diagnose a StackOverflowError. Note that the potential for stack overflow is not limited to Java.Diagnosing the cause of a StackOverflowError can be fairly straightfoward if the code has been compiled with the debug option
how to produce a StackOverflowError exception by doing recursive method calls. With the default setting, Java 8 can hold about 8900 frames in a stack. From the previous section, we understand that StackOverflowError exception occurs when a thread has too many frames in its stack and there is no room to add a new frame to make the next method call. The StackOverflowError exception is easy to produce by doing recursive method calls. Here is an example program, StackOverflowTest.java, that will end with a StackOverflowError exception: /* StackOverflowTest.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ class StackOverflowTest { static long n; public static void main(String[] a) { n = 0; try { sub(); } catch (StackOverflowError e) { e.printStackTrace(); System.out.println("Maximum nested calls: "+n); } } private static void sub() { n++; sub(); } } Compile and run StackOverflowTest.java, you will get this output: C:\herong>\progra~1\Java\jdk1.8.0\bin\javac StackOverflowTest.java C:\>\progra~1\Java\jdk1.8.0\bin\java StackOverflowTest 2> output.txt Maximum nested calls: 8906 C:\herong>type output.txt java.lang.StackOverflowError at StackOverflowTest.sub(StackOverflowTest.java:18) at StackOverflowTest.sub(StackOverflowTest.java:18) at StackOverflowTest.sub(StackOverflowTest.java:18) ... (1024 repeating lines) The output tells us that: StackOverflowError exception is stopping us running infinite recursive method calls. Java 8 default stack size is big enough to hold 8906 frames, meaning that a thread can make up to 8906 nested method calls. The printStackTrace() method can only print up to 1024 method calling code lines. Last update: 2014. Table of Contents About This Book Downloading and Installing JDK 1.8.0 on Windows Downloading and Installing JDK 1.7.0 on Windows java.lang.Runtime Class - The JVM Instance java.lang.System Class - The Operating System Class