java.lang.OutOfMemoryError: Java heap space (even with increased memory)

0

My code has this error. But I've already tried:

  • Increase memory in VM settings:
  •   

    # custom IntelliJ IDEA VM options

         

    -Xms512m   -Xmx1024m   -XX: ReservedCodeCacheSize = 480m   -XX: + UseConcMarkSweepGC   -XX: SoftRefLRUPolicyMSPerMB = 50   -and the   -Dsun.io.useCanonCaches = false   -Djava.net.preferIPv4Stack = true   -XX: + HeapDumpOnOutOfMemoryError   -XX: -OmitStackTraceInFastThrow

    The method in which this overflow occurs is this:

    public List<SearchByCity> getSearchesByCity(){
        List<SearchByCity> searches = new ArrayList<>();
        Arrays.asList(Estado.values()).forEach(uf -> {
            System.out.println(String.format("COLETANDO CIDADES EM %s", uf.getNome().toUpperCase()));
            try {
                List<String> cityNames = new ArrayList<>();
                HtmlPage page = WebClientFactory.getInstance().getPage(String.format(SEARCH_CITIES_URL, uf.toString().toLowerCase()));
                cityNames.addAll(Optional.ofNullable(page.<HtmlAnchor>getByXPath(CITYS_URLS_XPATH).stream()
                        .map(anc -> anc.getAttribute("title")).collect(Collectors.toList())).orElse(null));
                final JsonResponseByCity[] jsonResponseByCity = {null};
                if(Objects.nonNull(cityNames)){
                    cityNames.forEach(city -> {
                        String cityName = removeAccents(city.replaceAll(" ", "-").toLowerCase().replaceAll("'", ""));
                        String url = String.format(SEARCH_BY_CITY_URL, uf.getNome(), cargoPolitico.getDescription(), cityName, uf.toString().toLowerCase(), cityName, cargoPolitico.getDescription());
                        try {
                            jsonResponseByCity[0] = ObjectMapperFactory.getInstance().readValue(WebClientFactory.getInstance().getPage(url).getWebResponse().getContentAsString(), JsonResponseByCity.class);
                        } catch (IOException e) {
                            throw new UncheckedIOException(e);
                        }
                        searches.add(new SearchByCity(uf.getNome().toUpperCase(), cityName, cargoPolitico, jsonResponseByCity[0]));
                    });
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        });
        if(searches.size() > 0)
            return searches;
        return null;
    }
    

    The overflow happens in the line of the value assignment to jsonResponseByCity [0 ].

      

    java.lang.OutOfMemoryError: Java heap space

         

    at java.util.Arrays.copyOf (Arrays.java:3332) at   java.lang.AbstractStringBuilder.ensureCapacityInternal (AbstractStringBuilder.java:124)     at   java.lang.AbstractStringBuilder.append (AbstractStringBuilder.java:596)     at java.lang.StringBuilder.append (StringBuilder.java:190) at   org.apache.commons.io.output.StringBuilderWriter.write (StringBuilderWriter.java:143)     at org.apache.commons.io.IOUtils.copyLarge (IOUtils.java:2370) at   org.apache.commons.io.IOUtils.copyLarge (IOUtils.java:2348) at   org.apache.commons.io.IOUtils.copy (IOUtils.java:2325) at   org.apache.commons.io.IOUtils.copy (IOUtils.java:2273) at   org.apache.commons.io.IOUtils.toString (IOUtils.java:1041) at   com.gargoylesoftware.htmlunit.WebResponse.getContentAsString (WebResponse.java:246)     at   com.gargoylesoftware.htmlunit.WebResponse.getContentAsString (WebResponse.java:191)     at   br.crawler.impl.eleicoes.page.SearchBuilder.lambda $ null $ 2 (SearchBuilder.java:71)

        
    asked by anonymous 18.05.2018 / 14:18

    0 answers