使用Jackson ObjectMapper抛出OutOfMemoryError

时间:2018-08-22 12:09:48

标签: java android jackson realm objectmapper

我正在尝试从AsyncTask线程中的ObjectMapper获取JSON格式的字符串,而我一直在获取OutOfMemoryError 并且清单文件中已经添加了行

清单文件:

    android:largeHeap="true"
    android:hardwareAccelerated="false"

在doInBackground中执行的AsyncTask方法:

    String url = "/plano/fiscalizacao/fiscalizar";

    Realm realm = Realm.getDefaultInstance();


    Fiscalizacao fiscalizacao = RealmOperations.getFiscalizacaoById(realm, idFiscalizacao);

    if (fiscalizacao == null)
        return;

    ObjectMapper mapper = new ObjectMapperProvider().createDefaultObjectMapper();

    String json = mapper.writeValueAsString(fiscalizacao);

    StringEntity entity = new StringEntity(json, "UTF-8");
    //HttpEntity entity = new ByteArrayEntity(json.getBytes("UTF-8"));

    HttpResponse response = WebServiceSupport.post(context, url, entity);

    if (WebServiceSupport.validaStatus(response)) {
        String str = EntityUtils.toString(response.getEntity(), "UTF-8");
    }

Fiscalizacao类:

private static final long serialVersionUID = 1L;
@Nullable
private Date dataFiscalizacao;
@Nullable
private PlanoFiscalizacaoProcesso plano;
@Nullable
private String relatorio;
@Nullable
private Reparticao reparticao;
@PrimaryKey
private Long selfId;
@Nullable
private SyncStatus status_sync;
private TipoGrauAproveitamento tipoGrauAproveitamento;
private TipoMedida tipoMedida;
@Nullable
private Date versionDate;

问题所在的堆栈跟踪

08-22 14:04:12.623 22737-32404/exi.co.mz.sigit.fiscalizacao E/UncaughtException:
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 524300 byte allocation with 277842 free bytes and 271KB until OOM
    at com.fasterxml.jackson.core.util.TextBuffer._charArray(TextBuffer.java:716)
    at com.fasterxml.jackson.core.util.TextBuffer.expand(TextBuffer.java:675)
    at com.fasterxml.jackson.core.util.TextBuffer.append(TextBuffer.java:471)
    at com.fasterxml.jackson.core.io.SegmentedStringWriter.write(SegmentedStringWriter.java:67)
    at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeRaw(WriterBasedJsonGenerator.java:476)
    at com.fasterxml.jackson.core.util.DefaultPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultPrettyPrinter.java:382)
    at com.fasterxml.jackson.core.util.DefaultPrettyPrinter.writeEndObject(DefaultPrettyPrinter.java:247)
    at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeEndObject(WriterBasedJsonGenerator.java:179)
    at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.close(WriterBasedJsonGenerator.java:882)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2727)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2210)
    at exi.co.mz.fiscalizacao.rest.ParcelaClient.setFiscalizacao(ParcelaClient.java:184)
    at exi.co.mz.fiscalizacao.rest.ParcelaClient.doInBackground(ParcelaClient.java:239)
    at exi.co.mz.fiscalizacao.rest.ParcelaClient.doInBackground(ParcelaClient.java:33)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    at java.lang.Thread.run(Thread.java:818)

连续堆栈跟踪:

08-22 14:04:12.923 22737-32404/exi.co.mz.sigit.fiscalizacao E/AndroidRuntime:
FATAL EXCEPTION: AsyncTask #3
Process: exi.co.mz.sigit.fiscalizacao, PID: 22737
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

尝试遵循许多代码的标准:

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 524300 byte allocation with 277842 free bytes and 271KB until OOM
at com.fasterxml.jackson.core.util.TextBuffer._charArray(TextBuffer.java:716)
at com.fasterxml.jackson.core.util.TextBuffer.expand(TextBuffer.java:675)
at com.fasterxml.jackson.core.util.TextBuffer.append(TextBuffer.java:471)
at com.fasterxml.jackson.core.io.SegmentedStringWriter.write(SegmentedStringWriter.java:67)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeRaw(WriterBasedJsonGenerator.java:476)
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultPrettyPrinter.java:382)
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter.writeEndObject(DefaultPrettyPrinter.java:247)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeEndObject(WriterBasedJsonGenerator.java:179)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.close(WriterBasedJsonGenerator.java:882)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2727)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2210)
at exi.co.mz.fiscalizacao.rest.ParcelaClient.setFiscalizacao(ParcelaClient.java:184)
at exi.co.mz.fiscalizacao.rest.ParcelaClient.doInBackground(ParcelaClient.java:239)
at exi.co.mz.fiscalizacao.rest.ParcelaClient.doInBackground(ParcelaClient.java:33)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:818) 

1 个答案:

答案 0 :(得分:0)

得到解决方案

使用Realm时,必须使用.copyFromRealm()从领域数据库中提取对象

pipenv