(It’s the first time that I write an article about coding, there may exist some problems, I would appreciate sincerely if you can let me aware of that mistake. )
When we are coding these two kinds of occasions sometimes happens, however it may be a little bit strange for you to find the difference between the two expressions. Currently, I’m gonna tell you what should we deal with the two cases exactly.
As for the two occasions, we can say that the two errors are related in different areas of storing.
It is meant to be aware of the exact kinds in Java Virtual Machine:
1.Java Virtual Machine stacks
2.Heap area
3.Method area
4.Run time constant pool
5.Native method stacks
(The highlight sentences are the areas that we are discussing.)
When we start JVM, we have to define how much RAM it is gonna use when we are processing.
So for the different purposes for coding, JVM divides this into certain memory locations.
STACK & HEAP are two of them.
During the coding process, if you have a large object or referenced objects in memory, the “OutMemory Erro” takes place.
for (int i = 1; i > 0; i++)
vector.add(new BigObject());
If you have strong references to objects, the GC (Garbage Cycling) is unable to clear enough space for the object. When JVM tries to allocate memory for a new object and not enough space available, the " StackOverflow Erro" happens.
public void f(int x) {
return f(x + 1);
}
Sometimes these kinds of problems can’t be awarded in time, which means that it can be a serious problem that you are unable to pick it out and solve it. So the most efficient way to deal with the problem is even avoided making these problems.
The following are some of my ideas, I wish that some of them can help you.
(1)For the OutMemory Erro:
You should make sure that the GC is available for all the objects that you don’t need, or you can use the GC Algorithm directly if it is necessary.
(1)For the StackOverflow Erro:
Make sure that all the loop expressions have some limits to control their running times.
What’s more, you are supposed to figure out how many sources you need and keep communicating with your partners if you are working together.