AEM Guide

What Is a Sling ResourceResolver?

How Sling's ResourceResolver maps JCR content to Resources, why it must be closed, and how leaks happen in AEM.

slingjcrresourceresolver

A ResourceResolver is the Apache Sling API for resolving a path (or a request) into a Resource. It sits on top of a JCR Session and is the primary way AEM code reads and writes content.

Why It Must Be Closed

Every ResourceResolver obtained from a ResourceResolverFactory wraps a live JCR session. If you don’t close it, the underlying session — and the resources it holds (locks, caches, listeners) — stays alive until the garbage collector notices, which on a busy author or publish instance can mean thousands of leaked sessions and, eventually, OutOfMemoryError.

try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(params)) {
    Resource page = resolver.getResource("/content/we-retail/us/en");
    // work with the resource
} // resolver.close() is called automatically

Always acquire a ResourceResolver in a try-with-resources block (or an equivalent finally). This is the single most common source of resource leaks in custom AEM code.

Where This Comes Up