Redian新闻
>
How to: Abort DOM/XML loading when memory is low
avatar
How to: Abort DOM/XML loading when memory is low# Java - 爪哇娇娃
r*s
1
It is using DOM because of POI.
We are not changing the parsers -- too much work.
Assume we have no control of the file size, because the java
vm may be already low in memory, so a small one may blow it.
If it is getting "out of memory error", the whole app is gone.
So the question is whether we could do anything to prevent it
when the memory is low.
We would rather abort the xml loading than halt the whole app.
Any ideas?
avatar
o*i
2
my two cents:
create a thread to try loading the xml file (in try statement and maybe usin
g some "Hedge" techniques)
another thread monitoring the free memory for Runtime, when it lower than a
threshold value and loading still in processing, kill it and clean-up/releas
e the memory.

【在 r*****s 的大作中提到】
: It is using DOM because of POI.
: We are not changing the parsers -- too much work.
: Assume we have no control of the file size, because the java
: vm may be already low in memory, so a small one may blow it.
: If it is getting "out of memory error", the whole app is gone.
: So the question is whether we could do anything to prevent it
: when the memory is low.
: We would rather abort the xml loading than halt the whole app.
: Any ideas?

avatar
g*g
3
You should specify more memory on -Xmx.
You should have a limit for size of the file being processed, and a total
limit of the size of all files being processed concurrently.
You can also call Runtime.freeMemory() before you process the file. But it
may not be reliable.

【在 r*****s 的大作中提到】
: It is using DOM because of POI.
: We are not changing the parsers -- too much work.
: Assume we have no control of the file size, because the java
: vm may be already low in memory, so a small one may blow it.
: If it is getting "out of memory error", the whole app is gone.
: So the question is whether we could do anything to prevent it
: when the memory is low.
: We would rather abort the xml loading than halt the whole app.
: Any ideas?

avatar
F*n
4
One idea to use SoftReference for your Document.

【在 r*****s 的大作中提到】
: It is using DOM because of POI.
: We are not changing the parsers -- too much work.
: Assume we have no control of the file size, because the java
: vm may be already low in memory, so a small one may blow it.
: If it is getting "out of memory error", the whole app is gone.
: So the question is whether we could do anything to prevent it
: when the memory is low.
: We would rather abort the xml loading than halt the whole app.
: Any ideas?

avatar
F*n
5
More specifically, you can write a wrapper class for Document that uses a
SoftReference to contains an instance of Document implementation in the XML
API.
public class MyDocument implements Document {
private SoftReference docRef;
// do this for all method implementation
@Override
public Attr createAttribute(String name) {
Document doc = docRef.get();
if (doc == null) abort();
return doc.createAttribute(name);
}
}

【在 F****n 的大作中提到】
: One idea to use SoftReference for your Document.
avatar
g*e
6
soft ref也不能解决load某个文件过大导致OOM错吧

XML

【在 F****n 的大作中提到】
: More specifically, you can write a wrapper class for Document that uses a
: SoftReference to contains an instance of Document implementation in the XML
: API.
: public class MyDocument implements Document {
: private SoftReference docRef;
: // do this for all method implementation
: @Override
: public Attr createAttribute(String name) {
: Document doc = docRef.get();
: if (doc == null) abort();

avatar
F*n
7
It is guaranteed that soft refs will be gc-ed before an OOM exception is
thrown. If the document is gc-ed, the memory is released and the soft ref
will contain null.

【在 g**e 的大作中提到】
: soft ref也不能解决load某个文件过大导致OOM错吧
:
: XML

avatar
g*g
8
while the document is being processed, it's unlikely your soft ref is the
only ref. So it won't be GCed. And if it's not being processed, it should
have no reference in the system. soft ref is for cache, not for this.

【在 F****n 的大作中提到】
: It is guaranteed that soft refs will be gc-ed before an OOM exception is
: thrown. If the document is gc-ed, the memory is released and the soft ref
: will contain null.

avatar
F*n
9
The real document is wrapped inside the outer class.
Only the outer class will be hard referenced during parsing.

~~~~~ Now you learn it can do more.

【在 g*****g 的大作中提到】
: while the document is being processed, it's unlikely your soft ref is the
: only ref. So it won't be GCed. And if it's not being processed, it should
: have no reference in the system. soft ref is for cache, not for this.

avatar
g*e
10
这个我知道,但是这并不能100%保证在OOM之前full GC就kick off了

【在 F****n 的大作中提到】
: It is guaranteed that soft refs will be gc-ed before an OOM exception is
: thrown. If the document is gc-ed, the memory is released and the soft ref
: will contain null.

avatar
r*s
11
主要是这XML Loading是POI handle的,
要是JVM能在OOM之前就Throw a FailToAllocateMemoryError就好了

【在 g**e 的大作中提到】
: 这个我知道,但是这并不能100%保证在OOM之前full GC就kick off了
avatar
w*z
12
You can try to catch java.lang.OutOfMemoryError, but it might be too late at
that time and JVM is dying.
http://stackoverflow.com/questions/2679330/catching-java-lang-o
Before you load xml, can you check the size of the xml file?

【在 r*****s 的大作中提到】
: It is using DOM because of POI.
: We are not changing the parsers -- too much work.
: Assume we have no control of the file size, because the java
: vm may be already low in memory, so a small one may blow it.
: If it is getting "out of memory error", the whole app is gone.
: So the question is whether we could do anything to prevent it
: when the memory is low.
: We would rather abort the xml loading than halt the whole app.
: Any ideas?

avatar
r*s
13
We did, but
1. 这个File size跟DOM也不是正比的
2. When the jvm free heap is already running low,
even loading a small xml file may blow it up.

at

【在 w**z 的大作中提到】
: You can try to catch java.lang.OutOfMemoryError, but it might be too late at
: that time and JVM is dying.
: http://stackoverflow.com/questions/2679330/catching-java-lang-o
: Before you load xml, can you check the size of the xml file?

avatar
w*z
14
You need to scale up your system to solve the problem. Add more RAM to the
machine, enlarge heap. Or add more machines to your service.

【在 r*****s 的大作中提到】
: We did, but
: 1. 这个File size跟DOM也不是正比的
: 2. When the jvm free heap is already running low,
: even loading a small xml file may blow it up.
:
: at

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。