Skip to Content
NexPDF

Platform notes

Goroutines and threads, memory, linking, binary size and deployment notes for NexPDF for Go on servers and desktops.

English

Goroutines and threads

  • A *Document is not safe for concurrent use. Use each document from one goroutine at a time, or guard it with a sync.Mutex.
  • Different documents can be used from different goroutines at the same time, so goroutines 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 goroutine.
  • The license is process-wide: one ActivateLicense call covers every goroutine.
  • Errors are tracked per OS thread. Each SDK call locks its goroutine to its thread for the duration of the call, so an error never leaks into another goroutine.

A goroutine inside a cgo call holds an OS thread until the call returns, and the Go runtime starts more threads when many goroutines are in native code at once. Bound the number of concurrent NexPDF calls with a worker pool or a semaphore, sized to the CPU cores:

func renderFirstPages(paths []string) [][]byte {
	pngs := make([][]byte, len(paths))
	sem := make(chan struct{}, runtime.NumCPU())
	var wg sync.WaitGroup
	for i, path := range paths {
		wg.Add(1)
		go func(i int, path string) {
			defer wg.Done()
			sem <- struct{}{}
			defer func() { <-sem }()

			doc, err := nexpdf.Open(path) // one document per goroutine
			if err != nil {
				return
			}
			defer doc.Close()
			pngs[i], _ = doc.RenderPagePNG(0, 144)
		}(i, path)
	}
	wg.Wait()
	return pngs
}

Memory

  • A document is read fully into memory when it is opened. Budget roughly the file size plus the decoded pages you touch. Open reads the file with os.ReadFile, and the engine keeps its own copy, so the Go copy can be collected after the call.
  • 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 in 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.
  • Native memory is invisible to the Go garbage collector, and to GOMEMLIMIT. A finalizer frees a document you forget to close, but only when a collection happens to run. Call Close (usually with defer) as soon as you are done.
  • MemoryUsageBytes() and TrimMemory() let long-lived documents give memory back; see Compare and optimise.

Linking and size

  • The engine is one native library per platform: libnexpdf_ffi.so or libnexpdf_ffi.dylib, about 20 MB for one architecture (the macOS library is universal and holds two). Link it shared, and ship it with the program, or link the static libnexpdf_ffi.a into the binary. See Installation.
  • The static library removes the run-time dependency on libnexpdf_ffi, not on the C library: the program still needs glibc 2.35 or later on Linux.
  • 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.
  • Cross-compiling needs cgo for the target: a C cross-compiler (CC), the target's archive in CGO_LDFLAGS, and CGO_ENABLED=1. Building inside a container of the target architecture is usually simpler.
  • Linux (x86_64, arm64) and macOS are supported. cgo builds on Windows use MinGW, while the Windows library is built with MSVC; this combination is not verified.

Containers and serverless

Use a glibc-based image for both stages, for example golang:1.22-bookworm to build and debian:bookworm-slim to run; scratch and Alpine images do not work. The Installation page has a complete Dockerfile. Activate the license once at start-up, before the server accepts requests, so warm invocations reuse it. Work with OpenBytes and SaveBytes to stay in memory instead of writing temporary files.

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 AddLTV contact the servers named in the certificates.

On this page