Terminating due to java.lang.OutOfMemoryError: Metaspace (AsciiDoctorj-pdf)

119 Views Asked by At

I am currently attempting to deploy a JAR file that converts an Asciidoc file to a PDF file. However, when attempting to convert an Asciidoc file larger than 10kb, I am encountering an out of memory error related to Metaspace. Despite allocating 700MB to Metaspace, the issue persists. How can I resolve this error?

import org.asciidoctor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
import static org.asciidoctor.OptionsBuilder.options;

@RestController
public class Test {
  private static final String OUTPUT_DIR = new File(".").getAbsolutePath();
  @Autowired
  private HttpServletRequest request;

  @PostMapping("/test")
  public ResponseEntity<byte[]> handleFileUpload(@RequestParam("file") MultipartFile file)  {
    if (!file.isEmpty()) {
      try {
        //Copying th Uploaded file into the ./ Current Directory (./swaggerFile.yaml)
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        File uploadedFile = new File(OUTPUT_DIR + File.separator + fileName);
        file.transferTo(uploadedFile);

        Asciidoctor asciidoctor = Asciidoctor.Factory.create();

        Map<String, Object> options = options().inPlace(true)
          .backend("pdf")
          .asMap();

        asciidoctor.convertFile(uploadedFile,options);

        long maxMetaspace = Runtime.getRuntime().maxMemory();
        double maxMetaspaceInMB = (double) maxMetaspace / (1024 * 1024);
        System.out.println("Max Metaspace Size: " + maxMetaspaceInMB + " MB");

        String msg = "PDF CONVERTED";
        byte[] m = msg.getBytes();
        return new ResponseEntity<>(m,HttpStatus.OK);
      } catch (Exception e) {
        String errorMessage = "FAILED" + e.getMessage();
        byte[] errorMessageBytes = errorMessage.getBytes();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(errorMessageBytes, headers, HttpStatus.INTERNAL_SERVER_ERROR);
      }
    } else {
      String errorMessage = "File Should not be Empty ";
      byte[] errorMessageBytes = errorMessage.getBytes();
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
      return new ResponseEntity<>(errorMessageBytes, headers, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}
<dependency>
  <groupId>io.github.swagger2markup</groupId>
  <artifactId>swagger2markup</artifactId>
  <version>1.3.3</version>
</dependency>
<dependency>
  <groupId>org.asciidoctor</groupId>
  <artifactId>asciidoctorj</artifactId>
  <version>2.4.2</version>
</dependency>
<dependency>
  <groupId>org.asciidoctor</groupId>
  <artifactId>asciidoctorj-pdf</artifactId>
  <version>2.3.4</version>
</dependency>
0

There are 0 best solutions below