2

In my project there is a stackOVerFlow exception, they are creating objects inside for loop while iterating Map object.

I suspect that this complex object creation by calling other class methods inside loop is causing stackOverFlow.

Any thought on this would be greatly appreciated.

Below is the code in JAVA

 for (final Map.Entry<String, SomeBean> entry : actualBeanInfo.getPropertyMap().entrySet()) 
{
        final SomeBean property = entry.getValue();

        propName = property.getName();


        if (property.getGetter() == null) {
            continue;
        }

        if (ignoreProperty(property)) {
            continue;
        }

        Object value = SomeBeanUtilities.getBeanProperty(obj, property);
        value = SomeBeanUtilities.valueFromProperty(property, declaredType, null, value, context);
    }

I feel below two lines are causing error.

Object value = SomeBeanUtilities.getBeanProperty(obj, property);
        value = SomeBeanUtilities.valueFromProperty(property, declaredType, null, value, context);
4
  • 3
    Often stack overflows are caused by infinite recursion. What method is this code a part of? Is it possible that any of the methods you are calling call the method you are defining? Commented Feb 4, 2015 at 8:53
  • 4
    The message of the StackOverflowException shows a stack trace which gives a specific hint where the problem is. Commented Feb 4, 2015 at 8:54
  • 3
    I suggest trying to read the Stack of the StackOverflowError and trying to use the debugger to see why it happens. Commented Feb 4, 2015 at 8:56
  • It is exactly one line that calls the method causing the error. Without knowing the method... Commented Feb 4, 2015 at 10:38

1 Answer 1

1

Doing something in a loop (e.g. a while or for loop)` won't cause a stack overflow. Guaranteed. Loops don't cause new stack frames to be allocated.

Calling methods can, especially if the methods are directly or indirectly recursive. (You can get a stack overflow without recursion, but it is unlikely ... unless you have created threads with stacks that are too small.) The same applies to constructors.

However, knowing that doesn't get you very far. If you want to track down what is actually causing a stack overflow, you need to look at the stacktrace and the code of the methods that are involved. (The bodies of the methods ...)


I suspect that ...

Don't waste your time looking for bugs based on "suspicion". Gather and analyse the evidence and use that (and good old-fashioned logic) to direct your bug hunting.

Sign up to request clarification or add additional context in comments.

2 Comments

Does creating object inside loop has any memory overhead?? like this line in above code. Object value = SomeBeanUtilities.getBeanProperty(obj, property); i know from some of the stackOVerflow threads that it does not impact the performance but my question is about memory than performance. Declaring object outside the loop would be a better i feel so that same object will be getting assigned new values instead of a new object in every loop
"Does creating object inside loop has any memory overhead?". It has a bounded stack memory overhead ... unless the constructor body involves calling other methods or constructors. (Even constructor chaining is bounded.) Bounded stack memory usage means that stack overflow is highly unlikely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.