← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 5 of 19

Java में Performance Optimization

Performance optimization आपके program को तेज़ चलाना या कम memory उपयोग करवाना है, जैसे किसी bike को tune करना। आप बेहतर tools चुनते हैं और wasteful steps से बचते हैं।

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

java
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");
	}
}

Choosing Efficient Collections

काम के लिए सही collection type चुनना अनावश्यक lookup overhead सीमित करता है -- जब आप ज़्यादातर क्रमवार iterate या index करते हैं तो ArrayList उपयोग करें, और key से लगभग O(1) lookups चाहिए तो HashMap, बजाय जो भी collection सबसे परिचित हो उसे default रूप से उपयोग करने के।

उदाहरण: Choosing Efficient Collections

java
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"));
	}
}

Avoiding Object Allocation

अनावश्यक temporary object allocation को सीमित करना garbage collector को संभालनी पड़ने वाली workload घटाता है, क्योंकि हर short-lived object को आखिरकार scan और वापस पाना ही होता है -- एक pattern जिस पर hot loops के अंदर खासतौर पर ध्यान देना चाहिए जो लाखों बार चलते हैं।

उदाहरण: Avoiding Object Allocation

java
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());
	}
}

Thread Pool Configuration

किसी Executor के ज़रिए thread pool का उपयोग करना आपको काम की हर unit के लिए नया thread बनाने और destroy करने के बजाय worker threads के एक fixed set को कई tasks में reuse करने देता है, जो हर नए OS thread के साथ आने वाली असली startup और teardown लागत से बचाता है।

उदाहरण: Thread Pool Configuration

java
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");
	}
}

Lazy Initialization Pattern

Lazy initialization किसी महंगे operation -- database load, एक बड़ा object construction -- को तब तक टाल देता है जब तक असल में उसकी ज़रूरत न हो, बजाय startup पर बिना शर्त उस लागत को चुकाने के भले ही वह code path कभी उपयोग ही न हो।

उदाहरण: Lazy Initialization Pattern

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. loop के अंदर += से string बनाना, जो कई temporary objects बनाता है; StringBuilder का उपयोग करें।
  2. बिना measure किए अंदाज़े से optimize करना, जबकि एक profiler या benchmark दिखाता है कि समय असल में कहां खर्च हो रहा है।
  3. loop में LinkedList का उपयोग करके get(i) call करना, जो धीमा है क्योंकि हर call list में चलती है; ArrayList का उपयोग करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.