Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What cause ( stack overflow error , out of memory error ) and how to solve it ?

user-image
Question added by Adel Ezat Fawzy Ellozy , Webdeveloper. , Saudi Arabian Maritiem Sports Federation
Date Posted: 2017/04/07
Shadi Al Barhouch
by Shadi Al Barhouch , Developer , THALES GROUP

OitOfmemoreyError is thrown when the jvm is not able to free the heapdum of the JVM . There are three solutions

1- Increse the heapdump using -Xms=512M for example

2-Use -XX:PermSize since the jvm free 32M each shot and this increase it

--XX:PermSize=128M

3- Use JProfiler or MemoreyLeakDetector rool to see where are the instances that occupy the memorey

StackOverflow is thrown when there are infinit recursive call

Most developer make mistake by modifying broken condition after processing

Example:

//This may cause StackOverflow

Object obj = checkObject();

if(set.contans(obj)){

return;

}

callRecursive(obj,set);

set.add(obj);

//solution

Object obj = checkObject();

if(set.contans(obj)){

return;

}

set.add(obj);

callRecursive(obj,set);

 

 

Julien-Yusef Neidballa
by Julien-Yusef Neidballa , Trading Platform Software Engineer , Fidessa Group plc

Stack overflow is usually caused by unbounded recursion, whereas the Out of Memory error is the result of trying to use more space for program memory than is available in the heap (you may want to look at the -Xmxn command line switch)

StackOverflowError : the stack as OutOfMemoryError : the heap

Mohd Faisal
by Mohd Faisal , Sr. Mobile Application Developer(Android/Flutter) , Naqel Express

 

 

1# StackOverFlow Error

 

When a function call is invoked by a Java application, a stack frame is allocated on the call stack. The stack frame contains the parameters of the invoked method, its local parameters, and the return address of the method. If there is no space for a new stack frame then, the stackoverflow is thrown by the Java Virtual Machine (JVM).

 

For an example:

intcheckOverFlow(){inta=;System.out.println(a)checkOverFlow()}

 

checkOverFlow()calling itself again again,and when the space used to keep track of what functions you're in is filled up, you get the stack overflow error.Solution:-

1.Ifyour are calling any methodagain & again .Try to check if there is a stopping condition to ur recursive call.

2.avoid deep recursion.

3.Avoid large stack variables

 

2#OutOfMemoryError

Javamemory is separated into two different regions. These regions arecalled Heap space and Permgen.Theremost common reason for the  Outof Memory (heap error)erroris simple – you try to fit an XXL application into an S-sized Javaheap space. That is the application just requires more Java heapspace than available to it to operate normally. Other causes for thisOutOfMemoryError message are more complex and are caused by aprogramming error.

Solution:-

1.Data Volume:-The application was designed to handle a certain amount of users or a certain amount of data. When the number of users or the volume of data suddenly spikes and crosses that expected threshold, the operation which functioned normally before the spike ceases to operate and triggers the Out of Memory (heap error)error.

2.memory leaks:- A particular type of programming error will lead your application to constantly consume more memory. Every time the leaking functionality of the application is used it leaves some objects behind into the Java heap space. Over time the leaked objects consume all of the available Java heap space and trigger the already familiar  Out of Memory (heap error)error.

Amir ahmed Ibrahim
by Amir ahmed Ibrahim , Engineering Manager , talabat

Many reasons could cause out of memory exception, loading large amount of data and processing it in a very long iteration could cause the error, you may have a very long iteration to process or import some data without any GC.To solve this error, mainly good application or bulk code design, then you may enforce GC.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.