1

Records can be obtained from a website and displayed on the screen or the file can be downloaded in csv format. when the arrayList has more than 60 thousand registers it gives me the error

java.lang.OutOfMemoryError: Java heap space

what am I doing wrong ??

this is the code

HTML


    function getFile(){

       success: function (data){
                    if(data.estado === 'success'){
                    download(data.nombre,data.file);
                   }

    }

    function download(filename, data) {
        var element = document.createElement('a');
        element.setAttribute('href', data );
        element.setAttribute('download', filename);
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
    }

JAVA



    ArrayList<ExcedentesVO> listado = new ArrayList<ExcedentesVO>();
            try{
                listado = dao.getRegistrosFile();
                if(listado.size() > 0){
                    String file = crearArchivo(listado);
                    json.put("estado","success");
                    json.put("file", file);         
                }


        private String crearArchivo(List<ExcedentesVO> list) throws Exception {
            StringBuilder archivo = new StringBuilder();
            for (Object vo : list) {
                archivo.append(vo.getxxx+";"+vo.getxxxx+";"+vo.getxxxx+"\n");
            }

            byte[] bytesEncoded = Base64.encodeBase64(archivo.toString().getBytes());
            String file = csv+new String(bytesEncoded);
            return file;
        }

ERROR


    ]] Root cause of ServletException.
    java.lang.OutOfMemoryError: Java heap space
        at java.lang.StringCoding$StringDecoder.decode(StringCoding.java:149)
        at java.lang.StringCoding.decode(StringCoding.java:193)
        at java.lang.StringCoding.decode(StringCoding.java:254)
        at java.lang.String.<init>(String.java:546)
        at java.lang.String.<init>(String.java:566)
        Truncated. see log file for complete stacktrace
    > 

estebanjpc
  • 23
  • 6
  • Related: https://stackoverflow.com/questions/6748432/java-heap-space-out-of-memory – David Parks Jun 05 '20 at 23:13
  • Does this answer your question? [Error java.lang.OutOfMemoryError: GC overhead limit exceeded](https://stackoverflow.com/questions/1393486/error-java-lang-outofmemoryerror-gc-overhead-limit-exceeded) – Dmitry Pisklov Jun 06 '20 at 00:10

1 Answers1

2

The memory available to use in your Java program is finite, and this error is an indication that you've used up all the memory possible in your program. Java objects are tied to your memory space, so as you keep adding more records to your String, the more space it eats up. Depending on the size of each record, appending them to your String can eat through memory fast.

One solution to circumvent this issue, is to write the records in chunks to the final file as you iterate over the list of records. This will offload some of the task to your disk space instead of relying entirely on memory.

A quick example follows, using file writing:

File csv = new File( "xxx" );         
FileWriter writer = new FileWriter( csv );

for (Object vo : list) {
    writer.write(vo.getxxx+";"+vo.getxxxx+";"+vo.getxxxx+"\n");
}
  • 1
    Just for some constructive feedback, maybe some example code based on the initial one would help the OP to understand better. – madtyn Jun 05 '20 at 23:22