|
| 1 | +public class AotCachingDemo { |
| 2 | + |
| 3 | + public static void main(String[] args) { |
| 4 | + |
| 5 | + System.out.println("Program started"); |
| 6 | + |
| 7 | + long start1 = System.currentTimeMillis(); |
| 8 | + createObjectsWithoutCaching(); |
| 9 | + long end1 = System.currentTimeMillis(); |
| 10 | + |
| 11 | + System.out.println("Time without caching: " + (end1 - start1) + " ms"); |
| 12 | + |
| 13 | + long start2 = System.currentTimeMillis(); |
| 14 | + createObjectsWithCaching(); |
| 15 | + long end2 = System.currentTimeMillis(); |
| 16 | + |
| 17 | + System.out.println("Time with caching: " + (end2 - start2) + " ms"); |
| 18 | + } |
| 19 | + |
| 20 | + // Simulates repeated object creation (no caching) |
| 21 | + static void createObjectsWithoutCaching() { |
| 22 | + for (int i = 0; i < 1_000_000; i++) { |
| 23 | + HeavyObject obj = new HeavyObject("Data"); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Simulates caching (reuse same object) |
| 28 | + static void createObjectsWithCaching() { |
| 29 | + HeavyObject cached = new HeavyObject("Data"); |
| 30 | + |
| 31 | + for (int i = 0; i < 1_000_000; i++) { |
| 32 | + HeavyObject obj = cached; // reuse |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Simulated heavy object |
| 38 | +class HeavyObject { |
| 39 | + private String data; |
| 40 | + |
| 41 | + HeavyObject(String data) { |
| 42 | + this.data = data; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/* |
| 47 | +What changed: Previous vs New |
| 48 | +
|
| 49 | +Previous Java style: |
| 50 | +- Objects created repeatedly at runtime |
| 51 | +- No built-in caching at JVM level |
| 52 | +- Higher memory and CPU usage |
| 53 | +
|
| 54 | +New Java 26 concept: |
| 55 | +- AOT (Ahead-of-Time) Object Caching |
| 56 | +- JVM can reuse objects instead of recreating them |
| 57 | +- Improves startup and runtime performance |
| 58 | +
|
| 59 | +Why the new approach is better: |
| 60 | +- Reduces object creation overhead |
| 61 | +- Faster application startup |
| 62 | +- Better memory efficiency |
| 63 | +
|
| 64 | +Pros: |
| 65 | +1. Improved performance |
| 66 | + - Less object creation |
| 67 | +
|
| 68 | +2. Better memory usage |
| 69 | + - Reuses objects |
| 70 | +
|
| 71 | +3. Faster startup |
| 72 | + - Useful for large applications |
| 73 | +
|
| 74 | +4. JVM optimization |
| 75 | + - Handled internally |
| 76 | +
|
| 77 | +Cons: |
| 78 | +1. Not directly controllable in code |
| 79 | + - Mostly JVM-level feature |
| 80 | +
|
| 81 | +2. Conceptual understanding required |
| 82 | + - Not obvious from normal coding |
| 83 | +
|
| 84 | +3. Benefit depends on use case |
| 85 | + - More useful in large-scale apps |
| 86 | +
|
| 87 | +Best use case: |
| 88 | +- Large applications |
| 89 | +- High-performance systems |
| 90 | +- Repeated object creation scenarios |
| 91 | +
|
| 92 | +Note: |
| 93 | +This example simulates the idea of caching. |
| 94 | +Actual AOT caching is handled internally by JVM. |
| 95 | +
|
| 96 | +Compile and run: |
| 97 | +javac AotCachingDemo.java |
| 98 | +java AotCachingDemo |
| 99 | +*/ |
0 commit comments