简体   繁体   English

树反序列化时的SimpleFramework java.lang.StackOverflowError

[英]Simpleframework java.lang.StackOverflowError on tree deserializing

I have large xml tree with following structure: 我有具有以下结构的大型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>

I am trying to deserialize it using simpleframework : 我正在尝试使用simpleframework反序列化它:

Serializer serializer = new Persister();
SampleElem root = serializer.read(SampleElem.class, someStream, false);

After some time application stopped with error. 一段时间后,应用程序因错误而停止。 Log from device: 从设备登录:

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

Data is completely correct, input xml is fully valid. 数据是完全正确的,输入的xml是完全有效的。 Do you have any ideas? 你有什么想法?

Thanks! 谢谢!

UPDATE: 更新:

What about TreeStrategy? 那么TreeStrategy呢? How it should be used? 应该如何使用? Have you any examples? 你有例子吗?

You can't change the default stack size, or the stack size of an existing thread, on Android. 您无法在Android上更改默认堆栈大小或现有线程的堆栈大小。 You can, however, set the stack size at the time a thread is created. 但是,您可以在创建线程时设置堆栈大小。 Using the full constructor: 使用完整的构造函数:

public Thread(ThreadGroup group, Runnable runnable, String threadName, long stackSize)

you can set the stack size up to 256KB. 您可以将堆栈大小设置为最大256KB。

So the strategy in your case would be to create a new thread with a deep stack, and run this parsing code on that thread. 因此,您的策略是使用深堆栈创建一个新线程,然后在该线程上运行此解析代码。

Try increasing the memory including these arguments: 尝试增加包括以下参数的内存:

-Xmx1024m -Xss1024m

You can specify the amount of memory to dedicate. 您可以指定专用的内存量。

Change your stack size by passing the following JVM argument: 通过传递以下JVM参数来更改堆栈大小:

-Xss128m

Since, you're XML file is large and the parser seems to be doing a recursion (probably because <SampleElem> can have <SampleElem> as a child) stack size needs to be increased. 因为您的XML文件很大,并且解析器似乎正在进行递归(可能是因为<SampleElem>可以将<SampleElem>作为子级),所以需要增加堆栈大小。 The default size is 512 KB only. 默认大小仅为512 KB。

Note: If the parsing still fails with 128m; 注意:如果解析仍然失败,则为128m; keep doubling the size (like 256m, then 512m etc.) 继续增加一倍(例如256m,然后是512m等)

The most satisfactory solution in my case was rewrite my xml schema to linear structure. 在我看来,最令人满意的解决方案是将xml模式重写为线性结构。 It reduced total memory requirements for deserializing tree. 它减少了反序列化树的总内存需求。

Thanks all for comments. 谢谢大家的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM