How to accurately measure memory usage for iterative and recursive binary search algorithms in Java?

I am benchmarking the performance of iterative and recursive binary search algorithms in Java, specifically measuring both execution time and memory usage for different dataset sizes. However, I am encountering issues with accurately measuring memory usage during the execution of the search algorithms.

package backend;

import java.util.Arrays;

/**
 * Benchmark for the performance of iterative and recursive binary search algorithms.
 * It measures both execution time and memory usage for various dataset sizes.
 */
public class BinarySearchBenchmark {

    private static final long[] EXTRA_ARRAY = new long[10_000_000];

    static {
        Arrays.fill(EXTRA_ARRAY, 1000);
    }

    /**
     * The main method that runs the benchmarks for iterative and recursive binary search algorithms
     * for different dataset sizes.
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        long[] dataset = new long[100_000];
        for (long i = 0; i < dataset.length; i++) {
            dataset[(int) i] = i;
        }

        long[] testSizes = {1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 150_000, 200_000};

        System.out.printf("%-15s %-20s %-20s %-20s %-20s%n",
                "Input Size (n)", "Iterative Time (ms)", "Recursive Time (ms)",
                "Iterative Memory (MB)", "Recursive Memory (MB)");

        for (long size : testSizes) {
            long[] subDataset = Arrays.copyOf(dataset, (int) size);

            long iterativeTime = benchmark(() -> iterativeBinarySearch(subDataset, subDataset[(int) (size / 2)]));
            long iterativeMemoryUsed = measureMemoryUsage(() -> iterativeBinarySearch(subDataset, subDataset[(int) (size / 2)]));

            long recursiveTime = benchmark(() -> recursiveBinarySearch(subDataset, 0, (int) (subDataset.length - 1), subDataset[(int) (size / 2)]));
            long recursiveMemoryUsed = measureMemoryUsage(() -> recursiveBinarySearch(subDataset, 0, (int) (subDataset.length - 1), subDataset[(int) (size / 2)]));

            System.out.printf("%-15d %-20.2f %-20.2f %-20.2f %-20.2f%n",
                    size,
                    iterativeTime / 1_000_000.0,
                    recursiveTime / 1_000_000.0,
                    iterativeMemoryUsed / 1.0,
                    recursiveMemoryUsed / 1.0);
        }
    }

    /**
     * Performs the iterative binary search on the given array.
     *
     * @param array The array on which the search is performed
     * @param target The target number to search for
     */
    private static void iterativeBinarySearch(long[] array, long target) {
        long low = 0, high = array.length - 1;
        while (low <= high) {
            long mid = low + (high - low) / 2;
            if (array[(int) mid] == target) return;

            if (array[(int) mid] < target) low = mid + 1;
            else high = mid - 1;
        }
    }

    /**
     * Performs the recursive binary search on the given array.
     *
     * @param array The array on which the search is performed
     * @param low The lowest index of the search range
     * @param high The highest index of the search range
     * @param target The target number to search for
     * @return The index of the target number, or -1 if not found
     */
    private static int recursiveBinarySearch(long[] array, long low, long high, long target) {
        if (low > high) return -1;
        long mid = low + (high - low) / 2;
        if (array[(int) mid] == target) return (int) mid;

        if (array[(int) mid] < target) return recursiveBinarySearch(array, mid + 1, high, target);
        return recursiveBinarySearch(array, low, mid - 1, target);
    }

    /**
     * Measures the time it takes to run the given search function.
     *
     * @param search The search function to measure
     * @return The time in nanoseconds
     */
    private static long benchmark(Runnable search) {
        long startTime = System.nanoTime();
        for (int i = 0; i < 100; i++) {
            search.run();
        }
        return System.nanoTime() - startTime;
    }

    /**
     * Measures the memory usage during the execution of the given search function.
     *
     * @param search The search function to measure
     * @return The difference in memory usage in MB
     */
    private static long measureMemoryUsage(Runnable search) {
        System.gc();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        long beforeMemory = getUsedMemory();
        search.run();
        long afterMemory = getUsedMemory();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return (afterMemory - beforeMemory) / (1024 * 1024);
    }

    /**
     * Gets the used memory in the JVM in bytes.
     *
     * @return The used memory in bytes
     */
    private static long getUsedMemory() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }
}

The code uses System.gc() to trigger garbage collection and Thread.sleep(100) to allow GC to complete its work before measuring memory usage, but I am unsure whether this is accurately capturing the memory used by the search algorithms. Additionally, I am measuring memory usage before and after running the search 100 times, but I am unsure if the results reflect the true memory consumption of the algorithms.

Here are the methods I am using:

benchmark() – Measures execution time of the search algorithms by running them 100 times.
measureMemoryUsage() – Measures memory usage by comparing the used memory before and after the search algorithm execution, with garbage collection and a brief pause to let it complete.
Question:

How can I improve the accuracy of memory usage measurement for the iterative and recursive binary search algorithms in Java? Is there a more reliable way to measure memory usage during the execution of these algorithms?

What I Tried:

I used System.gc() to trigger garbage collection and Thread.sleep(100) to allow garbage collection to complete before measuring memory usage. This was done both before and after the search algorithm execution to capture the memory difference.
I measured memory usage by calculating the difference between the used memory before and after running the binary search 100 times, assuming that this would accurately reflect the memory consumed by the search algorithms.
I also tested both iterative and recursive binary search algorithms on various dataset sizes and measured both execution time and memory usage for comparison.
What I Expected:

I expected the measureMemoryUsage() method to give an accurate measurement of memory consumption during the execution of the binary search algorithms. I hoped to see a reliable difference in memory usage between the iterative and recursive implementations for different dataset sizes. The results would then help me compare the efficiency of the two approaches in terms of both time and memory usage.

New contributor

Ersin Karaduman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How to accurately measure memory usage for iterative and recursive binary search algorithms in Java?

I am benchmarking the performance of iterative and recursive binary search algorithms in Java, specifically measuring both execution time and memory usage for different dataset sizes. However, I am encountering issues with accurately measuring memory usage during the execution of the search algorithms.

package backend;

import java.util.Arrays;

/**
 * Benchmark for the performance of iterative and recursive binary search algorithms.
 * It measures both execution time and memory usage for various dataset sizes.
 */
public class BinarySearchBenchmark {

    private static final long[] EXTRA_ARRAY = new long[10_000_000];

    static {
        Arrays.fill(EXTRA_ARRAY, 1000);
    }

    /**
     * The main method that runs the benchmarks for iterative and recursive binary search algorithms
     * for different dataset sizes.
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        long[] dataset = new long[100_000];
        for (long i = 0; i < dataset.length; i++) {
            dataset[(int) i] = i;
        }

        long[] testSizes = {1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 150_000, 200_000};

        System.out.printf("%-15s %-20s %-20s %-20s %-20s%n",
                "Input Size (n)", "Iterative Time (ms)", "Recursive Time (ms)",
                "Iterative Memory (MB)", "Recursive Memory (MB)");

        for (long size : testSizes) {
            long[] subDataset = Arrays.copyOf(dataset, (int) size);

            long iterativeTime = benchmark(() -> iterativeBinarySearch(subDataset, subDataset[(int) (size / 2)]));
            long iterativeMemoryUsed = measureMemoryUsage(() -> iterativeBinarySearch(subDataset, subDataset[(int) (size / 2)]));

            long recursiveTime = benchmark(() -> recursiveBinarySearch(subDataset, 0, (int) (subDataset.length - 1), subDataset[(int) (size / 2)]));
            long recursiveMemoryUsed = measureMemoryUsage(() -> recursiveBinarySearch(subDataset, 0, (int) (subDataset.length - 1), subDataset[(int) (size / 2)]));

            System.out.printf("%-15d %-20.2f %-20.2f %-20.2f %-20.2f%n",
                    size,
                    iterativeTime / 1_000_000.0,
                    recursiveTime / 1_000_000.0,
                    iterativeMemoryUsed / 1.0,
                    recursiveMemoryUsed / 1.0);
        }
    }

    /**
     * Performs the iterative binary search on the given array.
     *
     * @param array The array on which the search is performed
     * @param target The target number to search for
     */
    private static void iterativeBinarySearch(long[] array, long target) {
        long low = 0, high = array.length - 1;
        while (low <= high) {
            long mid = low + (high - low) / 2;
            if (array[(int) mid] == target) return;

            if (array[(int) mid] < target) low = mid + 1;
            else high = mid - 1;
        }
    }

    /**
     * Performs the recursive binary search on the given array.
     *
     * @param array The array on which the search is performed
     * @param low The lowest index of the search range
     * @param high The highest index of the search range
     * @param target The target number to search for
     * @return The index of the target number, or -1 if not found
     */
    private static int recursiveBinarySearch(long[] array, long low, long high, long target) {
        if (low > high) return -1;
        long mid = low + (high - low) / 2;
        if (array[(int) mid] == target) return (int) mid;

        if (array[(int) mid] < target) return recursiveBinarySearch(array, mid + 1, high, target);
        return recursiveBinarySearch(array, low, mid - 1, target);
    }

    /**
     * Measures the time it takes to run the given search function.
     *
     * @param search The search function to measure
     * @return The time in nanoseconds
     */
    private static long benchmark(Runnable search) {
        long startTime = System.nanoTime();
        for (int i = 0; i < 100; i++) {
            search.run();
        }
        return System.nanoTime() - startTime;
    }

    /**
     * Measures the memory usage during the execution of the given search function.
     *
     * @param search The search function to measure
     * @return The difference in memory usage in MB
     */
    private static long measureMemoryUsage(Runnable search) {
        System.gc();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        long beforeMemory = getUsedMemory();
        search.run();
        long afterMemory = getUsedMemory();

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return (afterMemory - beforeMemory) / (1024 * 1024);
    }

    /**
     * Gets the used memory in the JVM in bytes.
     *
     * @return The used memory in bytes
     */
    private static long getUsedMemory() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }
}

The code uses System.gc() to trigger garbage collection and Thread.sleep(100) to allow GC to complete its work before measuring memory usage, but I am unsure whether this is accurately capturing the memory used by the search algorithms. Additionally, I am measuring memory usage before and after running the search 100 times, but I am unsure if the results reflect the true memory consumption of the algorithms.

Here are the methods I am using:

benchmark() – Measures execution time of the search algorithms by running them 100 times.
measureMemoryUsage() – Measures memory usage by comparing the used memory before and after the search algorithm execution, with garbage collection and a brief pause to let it complete.
Question:

How can I improve the accuracy of memory usage measurement for the iterative and recursive binary search algorithms in Java? Is there a more reliable way to measure memory usage during the execution of these algorithms?

What I Tried:

I used System.gc() to trigger garbage collection and Thread.sleep(100) to allow garbage collection to complete before measuring memory usage. This was done both before and after the search algorithm execution to capture the memory difference.
I measured memory usage by calculating the difference between the used memory before and after running the binary search 100 times, assuming that this would accurately reflect the memory consumed by the search algorithms.
I also tested both iterative and recursive binary search algorithms on various dataset sizes and measured both execution time and memory usage for comparison.
What I Expected:

I expected the measureMemoryUsage() method to give an accurate measurement of memory consumption during the execution of the binary search algorithms. I hoped to see a reliable difference in memory usage between the iterative and recursive implementations for different dataset sizes. The results would then help me compare the efficiency of the two approaches in terms of both time and memory usage.

New contributor

Ersin Karaduman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật