我有一个大型xml树,结构如下:
<?xml version="1.0"?>
<SampleElem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Children>
<SampleElem>
<Children/>
<Id>1</Id>
<Value>Test2</Value>
</SampleElem>
</Children>
<Id>-1</Id>
<Value>Test1</Value>
</SampleElem>
我正在尝试使用simpleframework反序列化它:
Serializer serializer = new Persister();
SampleElem root = serializer.read(SampleElem.class, someStream, false);
一段时间后,应用程序因错误而停止。从设备登录:
E/AndroidRuntime(13591): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(13591): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(13591): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(13591): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(13591): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(13591): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(13591): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(13591): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(13591): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(13591): at java.lang.Thread.run(Thread.java:1019)
E/AndroidRuntime(13591): Caused by: java.lang.StackOverflowError
E/AndroidRuntime(13591): at java.lang.StringBuilder.append(StringBuilder.java:217)
E/AndroidRuntime(13591): at java.lang.reflect.Modifier.toString(Modifier.java:285)
E/AndroidRuntime(13591): at java.lang.reflect.Method.toString(Method.java:538)
E/AndroidRuntime(13591): at java.lang.reflect.Method.equals(Method.java:361)
E/AndroidRuntime(13591): at org.apache.harmony.lang.annotation.AnnotationFactory.invoke(AnnotationFactory.java:307)
E/AndroidRuntime(13591): at $Proxy1.inline(Native Method)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.ElementListLabel.getConverter(ElementListLabel.java:160)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CacheLabel.getConverter(CacheLabel.java:280)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readVariable(Composite.java:604)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.read(Composite.java:201)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.read(Composite.java:148)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CompositeList.populate(CompositeList.java:175)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CompositeList.read(CompositeList.java:120)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.read(Composite.java:201)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.read(Composite.java:148)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CompositeList.populate(CompositeList.java:175)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CompositeList.read(CompositeList.java:120)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.read(Composite.java:201)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.read(Composite.java:148)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CompositeList.populate(CompositeList.java:175)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.CompositeList.read(CompositeList.java:120)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591): at org.simpleframework.xml.core.Compos
数据完全正确,输入xml完全有效。你有什么想法吗?
谢谢!
更新
TreeStrategy怎么样?它应该如何使用?你有什么例子吗?
答案 0 :(得分:1)
您无法在Android上更改默认堆栈大小或现有线程的堆栈大小。但是,您可以在创建线程时设置堆栈大小。使用完整的构造函数:
public Thread(ThreadGroup group, Runnable runnable, String threadName, long stackSize)
您可以将堆栈大小设置为256KB。
因此,您的策略是创建一个具有深层堆栈的新线程,并在该线程上运行此解析代码。
答案 1 :(得分:0)
尝试增加内存,包括这些参数:
-Xmx1024m -Xss1024m
您可以指定要专用的内存量。
答案 2 :(得分:0)
通过传递以下JVM参数来更改堆栈大小:
-Xss128m
因为,你的XML文件很大,并且解析器似乎正在进行递归(可能因为<SampleElem>
可能有<SampleElem>
作为子代)堆栈大小需要增加。默认大小仅为512 KB。
注意:如果解析仍然失败,128m;保持一倍的大小(如256米,然后512米等)
答案 3 :(得分:0)
在我的案例中,最令人满意的解决方案是将我的xml架构重写为线性结构。它减少了反序列化树的总内存需求。
感谢大家的评论。