In-Depth Advice for Tuning and Programming Java 8, 11, and Beyond
Scott Oaks

#Java
#JDK
#APIs
#JVM
#LTS
👨💻 کدنویسی و تست معمولاً دو تا حوزه تخصص جدا از هم در نظر گرفته میشن. تو این کتاب کاربردی، اسکات اوکس (Scott Oaks) که خبره جاواست، رویکردش اینه که هر کسی با جاوا کار میکنه باید درک عمیقی از رفتار کد توی ماشین مجازی جاوا (JVM) داشته باشه—از جمله تنظیماتی (Tunings) که احتمالا به پرفورمنس کمک میکنه. این ویرایش دوم که آپدیت شده، کمکت میکنه دانش عمیقی درباره پرفورمنس اپلیکیشنهای جاوا با استفاده از JVM و پلتفرم جاوا به دست بیاری.
🛠 هم توسعهدهندهها و هم مهندسان پرفورمنس یاد میگیرن که چطور از ویژگیها، ابزارها و فرآیندهای مختلف برای بهبود عملکرد نسخههای LTS جاوا 8 و 11 استفاده کنن. با اینکه تاکید روی نسخهها و فیچرهاییه که توی پروداکشن ساپورت میشن، این کتاب یه پیشنمایشی از تکنولوژیهای جدید و هیجانانگیز مثل کامپایل Ahead-of-Time و گاربیج کالکشنهای (Garbage Collections) آزمایشی هم داره.
• درک اینکه پلتفرمها و کامپایلرهای مختلف جاوا چطور روی پرفورمنس تاثیر میذارن
• یادگیری نحوه کار گاربیج کالکشن جاوا
• اعمال چهار اصل مهم برای گرفتن بهترین نتیجه از تست پرفورمنس
• استفاده از JDK و ابزارهای دیگه برای اینکه بفهمیم یه اپلیکیشن جاوا چطور داره کار میکنه
• به حداقل رسوندن تاثیر گاربیج کالکتور از طریق تیونینگ و تمرینهای برنامهنویسی
• حل مشکلات پرفورمنسی در APIهای جاوا • بهبود پرفورمنس اپلیکیشنهای دیتابیسمحور جاوا
📑 فهرست مطالب
📖 از پیشگفتار 🆕 چه چیزهایی در ویرایش دوم جدید است
📅 از زمان ویرایش اول، جاوا یه چرخه انتشار ششماهه با نسخههای طولانیمدت (LTS) دورهای رو پیش گرفته؛ این یعنی نسخههایی که الان ساپورت میشن و با انتشار این کتاب همزمانن، جاوا 8 و جاوا 11 هستن. با اینکه ویرایش اول جاوا 8 رو پوشش میداد، اون موقع خیلی جدید بود. این ویرایش تمرکزش روی جاوا 8 و 11 هست که خیلی بالغتر شدن، به همراه آپدیتهای اساسی روی گاربیج کالکتور G1 و Java Flight Recorder. همچنین به تغییرات رفتار جاوا توی محیطهای کانتینری (Containerized) هم توجه شده.
💡 این ویرایش ویژگیهای جدید پلتفرم جاوا رو پوشش میده، از جمله یه ابزار میکروبنچمارکینگ جدید (jmh)، کامپایلرهای جدید JIT، اشتراکگذاری دیتای کلاس اپلیکیشن، و ابزارهای جدید پرفورمنس—و همینطور پوشش ویژگیهای جدید جاوا 11 مثل رشتههای فشرده (Compact Strings) و الحاق رشتهها.
👥 چه کسانی باید (و نباید) این کتاب را بخوانند 🎯 این کتاب برای مهندسان پرفورمنس و توسعهدهندههایی طراحی شده که میخوان بفهمن جنبههای مختلف JVM و APIهای جاوا چطور روی پرفورمنس تاثیر میذارن. اگه آخر شب یکشنبهست، سایتت قراره دوشنبه صبح بالا بیاد و دنبال یه راه حل سریع برای مشکلات پرفورمنسی هستی، این کتاب به دردت نمیخوره.
🚀 اگه تازه وارد تحلیل پرفورمنس شدی و داری این کار رو با جاوا شروع میکنی، این کتاب میتونه کمکت کنه. قطعا هدف من اینه که اطلاعات و کانتکست کافی ارائه بدم تا مهندسان تازهکار بتونن بفهمن چطور اصول پایه تیونینگ و پرفورمنس رو روی یه اپلیکیشن جاوا پیاده کنن. اما، تحلیل سیستم یه حوزه وسیعه. منابع عالی زیادی برای تحلیل سیستم به صورت کلی وجود داره (و اون اصول طبیعتا برای جاوا هم صادقه)، و از این جهت، این کتاب قراره یه مکمل خوب برای اون متون باشه.
⚙️ ولی در سطح بنیادی، برای اینکه جاوا واقعا سریع اجرا بشه، نیاز به درک عمیقی از نحوه کار واقعی JVM (و APIهای جاوا) داری. صدها فلگ (Flag) برای تیونینگ جاوا وجود داره، و تیون کردن JVM نباید فقط اینطوری باشه که کورکورانه تست کنی و ببینی چی جواب میده. در عوض، هدف من اینه که دانش دقیقی درباره اینکه JVM و APIها دارن چیکار میکنن بهت بدم، با این امید که اگه بفهمی اون اجزا چطور کار میکنن، بتونی به رفتار خاص یه اپلیکیشن نگاه کنی و بفهمی چرا داره بد کار میکنه. با درک این موضوع، حذف رفتار نامطلوب (که باعث پرفورمنس بد میشه) تبدیل به یه کار ساده (یا حداقل سادهتر) میشه.
🤔 یه جنبه جالب کار پرفورمنس جاوا اینه که دولوپرها معمولا پیشزمینه خیلی متفاوتی نسبت به مهندسان گروه پرفورمنس یا QA دارن. من دولوپرهایی رو میشناسم که هزارتا متد عجیب و غریب تو APIهای کمکاربرد جاوا رو حفظن ولی نمیدونن فلگ Xmn- یعنی چی. و مهندسان تستی رو میشناسم که با تنظیم فلگهای مختلف گاربیج کالکتور میتونن شیره پرفورمنس رو بکشن بیرون ولی به زور میتونن یه برنامه "Hello, World" مناسب به زبان جاوا بنویسن.
⚖️ پرفورمنس جاوا هر دو این حوزهها رو پوشش میده: فلگهای تیونینگ برای کامپایلر و گاربیج کالکتور و غیره، و بهترین روشهای استفاده از APIها. پس من فرض رو بر این میذارم که درک خوبی از نحوه نوشتن برنامه در جاوا داری. حتی اگه علاقه اصلیت جنبههای برنامهنویسی جاوا نیست، من وقت نسبتا زیادی رو صرف بحث درباره برنامهها میکنم، از جمله برنامههای نمونهای که برای ارائه خیلی از نقاط داده (Data points) توی مثالها استفاده شدن.
🔍 با این حال، اگه علاقه اصلیت خودِ پرفورمنس JVM هست—یعنی چطور رفتار JVM رو بدون هیچ کدنویسی تغییر بدی—بخشهای زیادی از این کتاب هنوزم برات مفیده. راحت باش و از قسمتهای کدنویسی بگذر و روی بخشهایی تمرکز کن که برات جذابه. و شاید توی مسیر، یه بینشی هم درباره اینکه اپلیکیشنهای جاوا چطور میتونن روی پرفورمنس JVM تاثیر بذارن پیدا کنی و شروع کنی به پیشنهاد دادن تغییراتی به دولوپرها تا زندگی تست پرفورمنسِ خودت رو راحتتر کنن.
👨💻 درباره نویسنده
🏢 اسکات اوکس (Scott Oaks) یک آرشیتکت در شرکت اوراکل (Oracle) هست، جایی که روی پرفورمنس نرمافزارهای میانافزار (Middleware) اوراکل کار میکنه. قبل از پیوستن به اوراکل، سالها در سان مایکروسیستمز (Sun Microsystems) کار میکرد و توی تکنولوژیهای مختلفی تخصص داشت، از هسته SunOS گرفته تا برنامهنویسی شبکه و RPCها تا سیستمهای ویندوز و OPEN LOOK Virtual Window Manager. در سال ۱۹۹۶، اسکات تبدیل به مبلغ فنی (Evangelist) جاوا برای Sun شد و در سال ۲۰۰۱ به گروه پرفورمنس جاوا پیوست--که از اون موقع تمرکز اصلیش بوده. اسکات همچنین نویسنده کتابهای امنیت جاوا، تردهای جاوا، JXTA in a Nutshell و Jini in a Nutshell از انتشارات O’Reilly هست.
Coding and testing are generally considered separate areas of expertise. In this practical book, Java expert Scott Oaks takes the approach that anyone who works with Java should be adept at understanding how code behaves in the Java Virtual Machine—including the tunings likely to help performance. This updated second edition helps you gain in-depth knowledge of Java application performance using both the JVM and the Java platform.
Developers and performance engineers alike will learn a variety of features, tools, and processes for improving the way the Java 8 and 11 LTS releases perform. While the emphasis is on production-supported releases and features, this book also features previews of exciting new technologies such as ahead-of-time compilation and experimental garbage collections.
Table of Contents
Chapter 1. Introduction
Chapter 2. An Approach to Performance Testing
Chapter 3. A Java Performance Toolbox
Chapter 4. Working with the JIT Compiler
Chapter 5. An Introduction to Garbage Collection
Chapter 6. Garbage Collection Algorithms
Chapter 7. Heap Memory Best Practices
Chapter 8. Native Memory Best Practices
Chapter 9. Threading and Synchronization Performance
Chapter 10. Java Servers
Chapter 11. Database Performance Best Practices
Chapter 12. Java SE API Tips
From the Preface
Since the first edition, Java has adopted a six-month release cycle with periodic long-term releases; that means the current supported releases that coincide with publication are Java 8 and Java 11. Although the first edition covered Java 8, it was quite new at the time. This edition focuses on a much more mature Java 8 and Java 11, with major updates to the G1 garbage collector and Java Flight Recorder. Attention is also given to changes in the way Java behaves in containerized environments.
This edition covers new features of the Java platform, including a new microbenchmarking harness (jmh), new just-in-time compilers, application class data sharing, and new performance tools—as well as coverage of new Java 11 features like compact strings and string concatenation.
Who Should (and Shouldn’t) Read This Book
This book is designed for performance engineers and developers who are looking to understand how various aspects of the JVM and the Java APIs impact performance. If it is late Sunday night, your site is going live Monday morning, and you’re looking for a quick fix for performance issues, this is not the book for you.
If you are new to performance analysis and are starting that analysis in Java, this book can help you. Certainly my goal is to provide enough information and context that novice engineers can understand how to apply basic tuning and performance principles to a Java application.However, system analysis is a broad field. There are a number of excellent resources for system analysis in general (and those principles, of course, apply to Java), and in that sense, this book will ideally be a useful companion to those texts.
At a fundamental level, though, making Java go really fast requires a deep understanding of how the JVM (and Java APIs) actually work. Hundreds of Java tuning flags exist, and tuning the JVM has to be more than an approach of blindly trying them and seeing what works. Instead, my goal is to provide detailed knowledge about what the JVM and APIs are doing, with the hope that if you understand how those things work, you’ll be able to look at the specific behavior of an application and understand why it is performing badly. Understanding that, it becomes a simple (or at least simpler) task to get rid of undesirable (badly performing) behavior.
One interesting aspect to Java performance work is that developers often have a very different background than engineers in a performance or QA group. I know developers who can remember thousands of obscure method signatures on little-used Java APIs but who have no idea what the flag -Xmn means. And I know testing engineers who can get every last ounce of performance from setting various flags for the garbage collector but who can barely write a suitable “Hello, World” program in Java.
Java performance covers both of these areas: tuning flags for the compiler and garbage collector and so on, and best-practice uses of the APIs. So I assume that you have a good understanding of how to write programs in Java. Even if your primary interest is not in the programming aspects of Java, I do spend a fair amount of time discussing programs, including the sample programs used to provide a lot of the data points in the examples.
Still, if your primary interest is in the performance of the JVM itself—meaning how to alter the behavior of the JVM without any coding—then large sections of this book should still be beneficial to you. Feel free to skip over the coding parts and focus on the areas that interest you. And maybe along the way, you’ll pick up some insight into how Java applications can affect JVM performance and start to suggest changes to developers so they can make your performance-testing life easier.
Scott Oaks is an architect at Oracle Corporation, where he works on the performance of Oracle’s middleware software. Prior to joining Oracle, he worked for years at Sun Microsystems, specializing in many disparate technologies from the SunOS kernel to network programming and RPCs to windows systems and the OPEN LOOK Virtual Window Manager. In 1996, Scott became a Java evangelist for Sun and in 2001 joined their Java Performance group--which has been his primary focus ever since. Scott also authored O’Reilly’s Java Security, Java Threads, JXTA in a Nutshell, and Jini in a Nutshell titles.









