Skip to Content
NexPDF

Platform notes

Threading, native memory, packaging and deployment notes for NexPDF for Java on servers and desktops.

English

Threading

  • A NexPdfDocument is not thread-safe. Use each document from one thread at a time.
  • Different documents can be used on different threads at the same time, so threads scale across cores for independent files. The state shared between documents (the license and the renderer's caches) is thread-safe.
  • doc.copy() gives an independent document you can hand to another thread.
  • The license is process-wide: one activateLicense call covers every thread of the JVM.
  • Errors are tracked per thread, so an error on one thread never leaks into another.

JNA calls hold no Java lock, so an ExecutorService renders or converts in parallel:

ExecutorService pool = Executors.newFixedThreadPool(8);
List<Future<byte[]>> pngs = new ArrayList<>();
for (Path path : paths) {
    pngs.add(pool.submit(() -> {
        try (NexPdfDocument doc = NexPdfDocument.open(path)) {   // one document per task
            return doc.renderPagePng(0, 144);
        }
    }));
}

In a web application (Spring, Jakarta EE, Micronaut and so on), open a document per request instead of sharing one between requests, and activate the license once at application start-up.

Desktop apps

Engine calls block the calling thread until they finish. In a Swing or JavaFX app, run rendering, conversion and OCR on a background thread (a SwingWorker, a JavaFX Task or your own executor) and pass only the resulting bytes back to the UI thread.

Memory

  • A document is read fully into memory when it is opened. Budget roughly the file size plus the decoded pages you touch.
  • The engine allocates native memory, outside the Java heap. -Xmx does not limit it, and container memory limits must leave room for it next to the heap.
  • A rendered page at 144 dpi of an A4 page is about 1190 x 1684 pixels before PNG compression. Render large documents page by page instead of keeping every PNG.
  • The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by every document so a logo or font used in many files is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB on a process that renders many large, image-heavy files. Memory that stays high after a rendering batch is this cache being reused, not a leak.
  • Close documents with try-with-resources. A Cleaner frees unreachable ones too, but only after the garbage collector notices them, which a large heap with little pressure may delay.
  • memoryUsageBytes() and trimMemory() let long-lived documents give memory back; see Compare and optimise.

Packaging and size

  • The jar itself is plain Java plus JNA. The engine is one shared library per platform: libnexpdf_ffi.so, libnexpdf_ffi.dylib or nexpdf_ffi.dll, about 20 MB for one architecture (the macOS library is universal and holds two).
  • The default engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard fonts and PDF/A. NexPdf.hasBundledFonts() tells you which build you have.
  • To ship one jar for every platform, put the libraries under the JNA platform folders (linux-x86-64/, darwin-aarch64/ and so on) in your resources; see Installation. Each library you add grows the jar, so include only the platforms you deploy to.
  • In containers, keep the library outside the jar and set NEXPDF_LIB_PATH, as in the Docker example.
  • Android apps do not use this package; use the Android SDK.

Serverless

Container and function runtimes work like any Linux x86_64 or arm64 host, as long as the image has glibc 2.35 or later (see Troubleshooting). Ship the library in your deployment package or image, set NEXPDF_LIB_PATH, and activate the license once in a static initialiser or at start-up so warm invocations reuse it. Use /tmp for intermediate files, or work with openBytes and saveBytes to stay in memory.

Network

The engine makes no network calls of its own: license keys are verified offline and documents never leave the process. Only signing options you set (tsa_url for a timestamp, ltv for OCSP and CRL lookups) and NexPdf.addLtv contact the servers named in the certificates.

On this page