Skip to main content

Lesson 7 of 8

Mini Project: Library System - Part 10 (Memory & JPMS)

Apply JVM knowledge to the Library System - structure it as a multi-module JPMS project, add JVM health monitoring, implement a SoftReference-based book cache that GC can reclaim under memory pressure, and add GC stats to the reporting dashboard.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

JPMS Multi-Module Library

JPMS Multi-Module Library
// Module: com.priygop.library.model (module-info.java)
module com.priygop.library.model {
    exports com.priygop.library.model;  // Book, Genre, BookStatus, BorrowRecord
}

// Module: com.priygop.library.service (module-info.java)
module com.priygop.library.service {
    requires com.priygop.library.model;
    requires java.base;
    requires java.logging;
    exports com.priygop.library.service;  // LibraryService, CheckoutService
}

// Module: com.priygop.library.app (module-info.java)
module com.priygop.library.app {
    requires com.priygop.library.model;
    requires com.priygop.library.service;
    requires java.logging;
}

// ------ SoftReference-Based Cache ------
import java.lang.ref.SoftReference;
import java.util.*;

public class BookCache {
    // SoftRef: GC can reclaim if memory is low - perfect for caches
    private final Map<String, SoftReference<Book>> cache = new HashMap<>();

    public void put(String isbn, Book book) {
        cache.put(isbn, new SoftReference<>(book));
    }

    public Optional<Book> get(String isbn) {
        SoftReference<Book> ref = cache.get(isbn);
        if (ref == null) return Optional.empty();
        Book book = ref.get();            // null if GC reclaimed
        if (book == null) {
            cache.remove(isbn);           // clean up stale entry
            return Optional.empty();
        }
        return Optional.of(book);
    }

    public int size()              { return cache.size(); }
    public void evictStale()       { cache.entrySet().removeIf(e -> e.getValue().get() == null); }
}

// ------ JVM Health Reporter ------
import java.lang.management.*;

public class JVMHealthReporter {
    public static String getReport() {
        MemoryMXBean     mem     = ManagementFactory.getMemoryMXBean();
        ThreadMXBean     threads = ManagementFactory.getThreadMXBean();

        long heapUsedMB = mem.getHeapMemoryUsage().getUsed() / (1024 * 1024);
        long heapMaxMB  = mem.getHeapMemoryUsage().getMax()  / (1024 * 1024);

        long totalGcTime = ManagementFactory.getGarbageCollectorMXBeans().stream()
            .mapToLong(GarbageCollectorMXBean::getCollectionTime).sum();

        long[] deadlocks = threads.findDeadlockedThreads();

        return String.format("""
            === JVM Health ===
            Heap:      %d / %d MB (%.0f%% used)
            Threads:   %d live (peak: %d)
            GC time:   %d ms total
            Deadlocks: %s
            """,
            heapUsedMB, heapMaxMB, (heapUsedMB * 100.0 / heapMaxMB),
            threads.getThreadCount(), threads.getPeakThreadCount(),
            totalGcTime,
            deadlocks == null ? "none" : deadlocks.length + " DETECTED!"
        );
    }
}

Quick Quiz

Tip

Tip

Practice Mini Project Library System Part 10 Memory JPMS in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Mini Project: Library System - Part 10 (Memory & JPMS)

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Library System Part 10 Memory JPMS from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Mini Project Library System Part 10 Memory JPMS is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/8 done