简体   繁体   English

Java - outOfMemory - 堆空间

[英]Java - outOfMemory - Heap Space

So I'm trying to complete an exercise where I've been asked to implement a method that does a binary search in an ArrayList of objects.因此,我正在尝试完成一个练习,要求我实现一种在 ArrayList 对象中进行二进制搜索的方法。 From the exercise:从练习:

Binary search

In the Main-class, implement a method public static int binarySearch(ArrayList<Book> books, int searchedId), which searches the list it received as a parameter, for a book with an id variable that matches the value of searchedId variable it received as a parameter. If that book is found the method, should return the index it's located at, in the list it received as a parameter. If the book isn't found, the method should return the value -1.

The method must be implemented as a binary search, which assumes the list is ordered. You should also assume, that the ids towards the beginning of the list, are always smaller than the ids towards the end of the list.

I have created two methods, one to check whether the arraylist is sorted ( isItSorted ) and the other one that will perform the binary search if the aforementioned method evaluates to true ( binarySearch ).我创建了两种方法,一种是检查 arraylist 是否已排序( isItSorted ),另一种是在上述方法评估为真时执行二分搜索( binarySearch )。 Please see below:请看下面:

public static boolean isItSorted(ArrayList<Book> books) {
        ArrayList<String> boo = new ArrayList<>();
        String isItSorted = "";
        for (int i = 0; i < books.size(); i++) {
            for (int j = i + 1; j < books.size(); j++) {
                if (books.get(i).getId() < books.get(j).getId()) {
                    isItSorted = "true";
                    boo.add(isItSorted);
                } else {
                    isItSorted = "false";
                    boo.add(isItSorted);
                }
            }
        }
        if (!(boo.contains("false"))) {
            return true;
        }
        return false;
    }

    public static int binarySearch(ArrayList<Book> books, long searchedId) {
        if (searchedId < 0 || books.isEmpty()) {
            return -1;
        } else if (isItSorted(books)) {
            int start = 0;
            int end = books.size() - 1;
            int middle = (start + end) / 2;

            if (books.get(middle).getId() == searchedId) {
                return middle;
            } else if (books.get(middle).getId() > searchedId) {
                end = middle - 1;
            } else if (books.get(middle).getId() < searchedId) {
                start = middle + 1;
            }
            
            while (start <= end) {
                if (books.get(start).getId() == searchedId) {
                    return start;
                }
                start++;
            }
        }
        return -1;
    }

Inside these java files, there's a test package that tests whether my solution is correct or not.在这些 java 文件中,有一个测试 package 可以测试我的解决方案是否正确。 While 95% of the tests are successful, when it reaches the method below (where it compares the time of execution compared to my other method (linear search)), I get the error Java outOfMemory heap Space .虽然 95% 的测试是成功的,但当它到达下面的方法时(它将执行时间与我的其他方法(线性搜索)进行比较),我收到错误Java outOfMemory heap Space I use NetBeans.我使用 NetBeans。 I've already tried the JVM commands.我已经尝试过 JVM 命令。 My solution seems to work with every number of objects I've tried, so perhaps there's something wrong with the test code below?我的解决方案似乎适用于我尝试过的所有对象,所以下面的测试代码可能有问题?

@Test
    @Points("07-05.2")
    public void binarySearchIsFasterThanLinearSearch() throws Throwable {
        ArrayList<Book> books = generateBooks(10000);
        Collections.sort(books, (k1, k2) -> k1.getId() - k2.getId());

        int searched = 1000001;
        long bSearchStart = System.nanoTime();
        int binarySearchId = Searching.binarySearch(books, searched);
        long bSearchEnd = System.nanoTime();
        assertTrue("When binary search does not find what was searched for, it must return -1", binarySearchId == -1);
        long lSearchStart = System.nanoTime();
        int linearSearchId = Searching.linearSearch(books, searched);
        long lSearchEnd = System.nanoTime();
        assertTrue("When linear search does not find what was searched for, it must return -1", linearSearchId == -1);

        long bSearchTime = bSearchEnd - bSearchStart;
        long lSearchTime = lSearchEnd - lSearchStart;

        assertTrue("When there are 10000 books to search, and the searched book is not found, binary search should be a lot faster than linear search. Current this isn't so", bSearchTime * 2 < lSearchTime);
    }
        ArrayList<String> boo = new ArrayList<>();
        String isItSorted = "";
        for (int i = 0; i < books.size(); i++) {
            for (int j = i + 1; j < books.size(); j++) {
                if (books.get(i).getId() < books.get(j).getId()) {
                    isItSorted = "true";
                    boo.add(isItSorted);
                } else {
                    isItSorted = "false";
                    boo.add(isItSorted);
                }
            }
        }

Adds on the order of 100 million items to the ArrayList boo. ArrayList boo 的订单增加了 1 亿件。

If you want to check if something is sorted you can use much simpler code:如果您想检查某些内容是否已排序,您可以使用更简单的代码:

Book prev = books[0];
for (int i = 1; i < books.size(); i++) {
   if (prev.getId() > books[i].getId()) 
      return false;
}
return true;

But you shouldn't need to call it inside binarySearch() because that will defeat the purpose of binarySearch() and make it as slow as linearSearch().但是您不需要在 binarySearch() 中调用它,因为这会破坏 binarySearch() 的目的,并使其与 linearSearch() 一样慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM