'Java Servlet writing a huge JSONArray response

I have the following java action method to get a Huge JSONArray as a response , we loading this data in Grid in UI , DAO data is generated from a Graph DB and there is no option for pagination , whole data will get in each request , It works with 1.5 Million data , but when trying with 3 Million data the response writing not getting completed. In Client side dev tools the ajax call not getting any response and its in infinite waiting, but after few minutes network call showing completed with empty response. After reloading the page everything works , not crashing.

public String loadSystemImpactGrid() throws Exception {
    SystemEnvironmentHelper.logPrint("Grid Data Action Starts ");
   
    HttpSession session = request.getSession(false);
    UserDetails userDetails = ((UserDetails) session.getAttribute(USER_DETAILS));
    SystemEnvironmentHelper systemEnvironmentHelper = new SystemEnvironmentHelper(userDetails);
    String gridType = request.getParameter("gridtype");
    JSONObject gridParamJson = new JSONObject(request.getParameter("gridreqdata"));
    String systemId = gridParamJson.getString("sysId");
    String systemName = gridParamJson.getString("systemName");
    String environmentName = gridParamJson.getString("envName");
    String tableName = gridParamJson.getString("tableName");
    String columnName = gridParamJson.getString("columnName");
    String impactAnalysisObjectType = gridParamJson.getString("impactAnalysisObjType");
    String projectIds = "";


    JSONArray impactArray = new JSONArray();
    impactArray = systemEnvironmentHelper.getImpactListData(systemName, environmentName, tableName, columnName, projectIds, gridType, impactAnalysisObjectType);  
    System.out.println("Current Buffer Size : "+response.getBufferSize());

   /*Getting Stuck After this , its working with 1.5 Million Data but not with 3 Million, */
        try (PrintWriter printWriterObj = response.getWriter()) {
        response.setContentType("application/json");
        printWriterObj.print(impactArray);
    }
    
    
    SystemEnvironmentHelper.logPrint("Grid Data Action Ends ");//Not Reaching this line
    return null;
}

I tried a ServletOutputStream example below but same result. Please Help with some alternative suggestion to write such huge response from Servlet.

    ServletOutputStream ostream = null;      
    InputStream istream = new ByteArrayInputStream(impactArray.toString().getBytes());
    try {  
    ostream = response.getOutputStream();
    int len;
    final byte buffer[] = new byte[2048];
    while ((len = istream.read(buffer)) != -1) {
        ostream.write(buffer, 0, len);
    }
    } finally {
        istream.close();
    }
    ostream.flush();

I am using 32GB Ram , and i7 8th Gen Processor , with -Xms10240m -Xmx20480m (Allocated 20GB tomcat heap memory) , Whole processing of data happeningfast (20Seconds or so for 3million).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source