Java में Performance Optimization
In this page:
Tuning JVM Flags
-Xms (initial heap size) और -Xmx (maximum heap size) जैसे JVM heap flags को adjust करना आपको यह tune करने देता है कि आपका application कितनी memory से शुरू हो और कितनी बढ़ सके, जो मायने रखता है क्योंकि बहुत छोटी heap बार-बार GC pauses का कारण बनती है और बहुत बड़ी heap system memory बर्बाद करती है।
उदाहरण: Tuning JVM Flags
public class Main {
public static void main(String[] args) {
// Run with: java -Xms256m -Xmx512m Main
Runtime runtime = Runtime.getRuntime();
System.out.println("Max heap: " + runtime.maxMemory() / (1024 * 1024) + "MB");
}
}
Login to try C/C++/Java/PHP code in the editor
Choosing Efficient Collections
काम के लिए सही collection type चुनना अनावश्यक lookup overhead सीमित करता है -- जब आप ज़्यादातर क्रमवार iterate या index करते हैं तो ArrayList उपयोग करें, और key से लगभग O(1) lookups चाहिए तो HashMap, बजाय जो भी collection सबसे परिचित हो उसे default रूप से उपयोग करने के।
उदाहरण: Choosing Efficient Collections
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(); // fast sequential iteration/indexing
Map<String, Integer> map = new HashMap<>(); // near O(1) lookup by key
list.add(1);
map.put("key", 1);
System.out.println(list.get(0) + " " + map.get("key"));
}
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Object Allocation
अनावश्यक temporary object allocation को सीमित करना garbage collector को संभालनी पड़ने वाली workload घटाता है, क्योंकि हर short-lived object को आखिरकार scan और वापस पाना ही होता है -- एक pattern जिस पर hot loops के अंदर खासतौर पर ध्यान देना चाहिए जो लाखों बार चलते हैं।
उदाहरण: Avoiding Object Allocation
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); // reuses one buffer instead of many temporary Strings
for (int i = 0; i < 1000; i++) {
sb.append(i);
}
System.out.println(sb.length());
}
}
Login to try C/C++/Java/PHP code in the editor
Thread Pool Configuration
किसी Executor के ज़रिए thread pool का उपयोग करना आपको काम की हर unit के लिए नया thread बनाने और destroy करने के बजाय worker threads के एक fixed set को कई tasks में reuse करने देता है, जो हर नए OS thread के साथ आने वाली असली startup और teardown लागत से बचाता है।
उदाहरण: Thread Pool Configuration
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(4); // reused workers, not a new thread per task
for (int i = 0; i < 8; i++) pool.submit(() -> {});
pool.shutdown();
System.out.println("8 tasks handled by 4 reused threads");
}
}
Login to try C/C++/Java/PHP code in the editor
Lazy Initialization Pattern
Lazy initialization किसी महंगे operation -- database load, एक बड़ा object construction -- को तब तक टाल देता है जब तक असल में उसकी ज़रूरत न हो, बजाय startup पर बिना शर्त उस लागत को चुकाने के भले ही वह code path कभी उपयोग ही न हो।
उदाहरण: Lazy Initialization Pattern
public class Main {
static class ExpensiveResource {
private static ExpensiveResource instance;
static ExpensiveResource get() {
if (instance == null) instance = new ExpensiveResource(); // deferred until first actually needed
return instance;
}
}
public static void main(String[] args) {
System.out.println(ExpensiveResource.get() != null);
}
}
Login to try C/C++/Java/PHP code in the editor
- loop के अंदर
+=से string बनाना, जो कई temporary objects बनाता है;StringBuilderका उपयोग करें। - बिना measure किए अंदाज़े से optimize करना, जबकि एक profiler या benchmark दिखाता है कि समय असल में कहां खर्च हो रहा है।
- loop में
LinkedListका उपयोग करकेget(i)call करना, जो धीमा है क्योंकि हर call list में चलती है;ArrayListका उपयोग करें।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing