Optimize Java Backend: Memory, JSP Caching, and Faster Collections

This article presents practical Java backend optimization techniques, covering servlet session memory management, JSP buffering, include strategies, efficient HashMap traversal, array versus ArrayList choices, and best practices for strings, splits, and general code performance.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
Optimize Java Backend: Memory, JSP Caching, and Faster Collections

21. Servlet and Memory Usage: Developers often store large objects in HttpSession, leading to memory spikes when garbage collection doesn't reclaim them. Two solutions: implement HttpSessionBindingListener in session-scoped beans to release resources in valueUnbound(), or invalidate sessions promptly using setMaxInactiveInterval() or server timeout settings.

22. Using Buffer Tags: Some application servers (e.g., WebLogic 6+, OpenSymphony) support JSP buffer tags that cache page fragments or whole pages, reducing execution for repeated requests such as shopping carts, catalogs, and portal homepages.

23. Choosing Include Mechanism: JSP can include external resources via the compile-time <%@ include file="..."%> directive or the runtime action. The directive merges files before compilation, while the action includes the result at request time, useful when the included content changes frequently.

24. Timely Session Cleanup: Default session timeout is usually 30 minutes. When memory is low, servers may swap inactive sessions to disk or serialize them, which is costly. Call HttpSession.invalidate() when a session is no longer needed.

26. HashMap Traversal Efficiency: Iterating over a HashMap using entrySet() is faster than iterating over keySet() and calling get() for each key because the former accesses entries directly without extra hash lookups.

Map<String, String[]> paraMap = new HashMap<String, String[]>();
Set<String> appFieldDefIds = paraMap.keySet();
for (String appFieldDefId : appFieldDefIds) {
String[] values = paraMap.get(appFieldDefId);
}
for (Entry<String, String[]> entry : paraMap.entrySet()) {
String appFieldDefId = entry.getKey();
String[] values = entry.getValue();
}

27. Array vs. ArrayList: Arrays are most efficient but have fixed size; ArrayList offers dynamic growth at the cost of performance. Use arrays when size is known; otherwise, use ArrayList.

28. Prefer HashMap and ArrayList over Hashtable and Vector unless synchronization is required, as the latter incur performance overhead.

29. StringBuffer vs. StringBuilder: StringBuilder is faster because it is not synchronized; use StringBuilder when thread safety is not needed, otherwise use StringBuffer.

30. Avoid using split() frequently because it relies on regular expressions; consider Apache StringUtils.split() or cache results.

Additional optimization tips: set unused objects to null, use final/static where possible, employ buffered streams, enable javac -O, extract common code, avoid large array initializations, store dates as long, shorten identifiers, place static final constants in interfaces, replace multiplication/division with bit shifts, consolidate try‑catch blocks, and move invariant code out of loops.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaoptimizationJSPmemory-management
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.