2
listaDecretoSingolo = new ArrayList<>();
listaDettaglioDecretoSingolo = new ArrayList<>();

for(int i = 0; i < 1022; i++){
    decretoSingoloTemp = new DecretoSingoloViewObject("Roma", "ABCDE3593CXXE",
    "decreto singolo", date,new BigDecimal(56000), new BigDecimal(343434),
    new BigDecimal(55656), new BigDecimal(9999));

    for(int j = 0; j < 111; j++){
        listaDettaglioDecretoSingolo.add(new DettaglioDecretoSingoloViewObject(22,
                                date, "numero dec pag",
                                "tipo pag", new BigDecimal(45),
                                "nota dec"));
                    }
        decretoSingoloTemp.setDetails(listaDettaglioDecretoSingolo);
        listaDecretoSingolo.add(decretoSingoloTemp);
}
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String jsonList = null;
jsonList = ow.writeValueAsString(listaDecretoSingolo);

I want test the speed of this code above. But, when I run it, and it tries to convert the list into a JSON string I get this exception:

at com.fasterxml.jackson.core.util.TextBuffer.carr(TextBuffer.java:703) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.TextBuffer.expand(TextBuffer.java:664) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.TextBuffer.append(TextBuffer.java:455) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.io.SegmentedStringWriter.write(SegmentedStringWriter.java:67) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._flushBuffer(WriterBasedJsonGenerator.java:1910) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeRawLong(WriterBasedJsonGenerator.java:496) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeRaw(WriterBasedJsonGenerator.java:430) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultPrettyPrinter.java:472) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter.beforeObjectEntries(DefaultPrettyPrinter.java:265) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._writePPFieldName(WriterBasedJsonGenerator.java:297) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._writeFieldName(WriterBasedJsonGenerator.java:227) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeFieldName(WriterBasedJsonGenerator.java:116) [jackson-core-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:539) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:809) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:684) [jackson-databind-2.3.3.jar:2.3.3]
at it.istruzione.hp.scuoleSicure.controller.ConsultaPagamentiController.ricercaConsultaImpegni(ConsultaPagamentiController.java:116) [classes:]
at it.istruzione.hp.scuoleSicure.controller.ConsultaPagamentiController$$FastClassBySpringCGLIB$$8a5debc0.invoke(<generated>) [spring-core-4.0.5.RELEASE.jar:]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708) [spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.0.5.RELEASE.jar:4.0.5.RELEASE]

I don't get this error when the list has a small size (like 10, 20, 30..). But, when I increase the size, near thousands or little less, I have this problem...

There is any way to fix it?

0

2 Answers 2

1

You are creating a lot of objects, so you need a lot of memory. Try using the -Xmx option to increase maximum heap size.

Xmx and Xms are well explained here.

Sign up to request clarification or add additional context in comments.

1 Comment

And when I publish the war on the server that's not mine (like the client server)?
1

Try to write json as ObjectWritter#writeValueAsBytes or ObjectWritter#writeValue(OutputStream out,Object value)

Writing as String is highly ineffective

Something like this:

Bar.java

package org.ibm.jackson.example;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

    public class Bar {
        private String id;

        @JsonCreator
        public Bar(@JsonProperty("id") String id) {
            this.id = id;
        }

        public String getId() {
            return this.id;
        }

        @Override
        public String toString() {
            return "Bar [id=" + id + "]";
        }

    }

JacksonMain.java

package org.ibm.jackson.example;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class JacksonMain {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException  {

        String json = "{\"id\": \"foo\"}";
        ObjectMapper mapper = new ObjectMapper();
        final Bar bar = mapper.readValue(json, Bar.class);
        //Read json
        System.out.println(bar);

        ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter();
        byte[] barWritten = objectWriter.writeValueAsBytes(bar);
        //Written json
        System.out.println(new String(barWritten));  //Just to show - you should write something like ByteArrayInputStream bis = new ByteArrayInputStream(barWritten);

    }

}

1 Comment

Do you mean that I should convert the list in byte array before, and then from byte array to JSON?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.