The difference between "OutMemory Erro "& "StackOverflow Erro"

本文探讨了Java编程中常见的内存错误,如OutOfMemoryError和StackOverflowError,详细解析了这些错误的发生原因,并提供了预防措施,包括合理使用垃圾回收机制和控制循环表达式的运行时长。

(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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值