0

I have a Spring Boot Application that has a scheduled task every two minutes to fetch data from time series and perform some calculations on it via an HTTP request and then store the result back in to time series.

The problem is that with each iteration the memory consumption is increasing. I have tried 2 GB and 4 GB memory for this but its runs out of memory after some time giving heap out memory error. Below is a sample code to give you a general idea of what I am doing.

@Scheduled(cron = "0 0/2 * * * ?")
public void run() {

    try {
        log.info(new Timestamp(System.currentTimeMillis()).toString() + " -- Starting analytics execution.");
        AnalyticResponseModel timeseriesResponse = null;

        //Get input for Analytics Execution
        timeseriesResponse = retrieveDataPointsCurrent(TagsDataBuffer.TAGS);

        //Prepare payload for model execution request
        String payload = mapper.writeValueAsString(timeseriesResponse);
        RequestBody requestBody = RequestBody.create(JSON, payload);
        Request request = new Request.Builder().url(analyticModelURL).header("Content-Type", "application/json")
                .header("Accept", "application/json").post(requestBody).build();
        if( timeseriesResponse.getData().getTime_series().get("time_stamp").isEmpty()) {
            log.error("No Recent Data");
            return;
        }
        dataTimestamp = (long) timeseriesResponse.getData().getTime_series().get("time_stamp").get(0);

        log.info(new Timestamp(System.currentTimeMillis()).toString() + " -- Fetching Input data.");

        //Execute request
        Response response = client.newCall(request).execute();
        parseAndSaveOutput( response);
    } catch (Exception e) {
        log.error(e.getMessage());
    }
}

1- How can I inspect where I am leaking the memory and how to do it in cloud foundry 2- Any alternate/better approach

trincot
  • 317,000
  • 35
  • 244
  • 286
Ehsan Waris
  • 148
  • 2
  • 12
  • How do you store data in parseAndSaveOutput() ? – Mikita Herasiutsin Oct 29 '18 at 12:25
  • AnalyticResponseModel arm = mapper.readValue(response.toString(), AnalyticResponseModel.class); I parse it and save to time series using their api. – Ehsan Waris Oct 29 '18 at 12:39
  • do you want me to share that method as well? – Ehsan Waris Oct 29 '18 at 12:39
  • After reviewing the code and trying out different things, it seems like the code for ingesting data to time series was leaking memory. I was initializing the **tenant** on each iteration and for some reason it was not being garbage collected. So, I tried **singleton approach** for this and the memory now seems to be **under control** & **resolved** . It has not exceeded from 1.3 GB since last week. – Ehsan Waris Nov 06 '18 at 06:03

1 Answers1

0

After reviewing the code and trying out different things, it seems like the code for ingesting data to time series was leaking memory. I was initializing the tenant on each iteration and for some reason it was not being garbage collected. So, I tried singleton approach for this and the memory now seems to be under control & resolved . It has not exceeded from 1.3 GB since last week

Ehsan Waris
  • 148
  • 2
  • 12