2016-10-02 3 views
0

8GB 램이있는 64 비트 windows10 운영 체제를 사용하고 있습니다. 내 eclipse.ini 파일은 다음과 같습니다.64 비트 Windows10에서 Java 프로세스 당 최대 메모리 양?

-startup 
plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar 
--launcher.library 
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.300.v20150602-1417 
-product 
org.eclipse.epp.package.jee.product 
--launcher.defaultAction 
openFile 
--launcher. 
-XX:MaxPermSize 
-Xms4000m 
-Xmx8000m 
-showsplash 
org.eclipse.platform 
--launcher.XXMaxPermSize 
-Xms4000m 
-Xmx8000m 
--launcher.defaultAction 
openFile 
-vm C:\Program Files\Java\jre1.8.0_91\bin\javaw.exe 
--launcher.appendVmargs 
-vmargs 
-Dosgi.requiredJavaVersion=1.7 
-Xms4000m 
-Xmx8000m 

이진 검색 트리에 100000 값을 삽입하려고합니다. BST에서 10000 값을 입력하려고하면 코드가 제대로 작동하지만 BST에 100000 값을 삽입하려고하면 JVM 힙 크기 문제가 발생합니다. 또한 단계 다음 수행 한 - VM 인수 섹션 에서 - - 인수 로 이동 - 구성 를 실행하는 이동 내가 -Xms4000m -Xmx8000m을 추가 한

다음과

내 코드가 같이

public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> 
{ 
/** 
    * Construct the tree. 
    */ 
public BinarySearchTree() 
{ 
    root = null; 
} 

/** 
    * Insert into the tree; duplicates are ignored. 
    * @param x the item to insert. 
    */ 
public void insert(AnyType x) 
{ 
    root = insert(x, root); 
} 

/** 
    * Remove from the tree. Nothing is done if x is not found. 
    * @param x the item to remove. 
    */ 
public void remove(AnyType x) 
{ 
    root = remove(x, root); 
} 

/** 
    * Find the smallest item in the tree. 
    * @return smallest item or null if empty. 
    */ 
public AnyType findMin() 
{ 
    if(isEmpty()) 
     return null; 
    return findMin(root).element; 
} 

/** 
    * Find the largest item in the tree. 
    * @return the largest item of null if empty. 
    */ 
public AnyType findMax() 
{ 
    if(isEmpty()) 
     return null; 
    return findMax(root).element; 
} 

/** 
    * Find an item in the tree. 
    * @param x the item to search for. 
    * @return true if not found. 
    */ 
public boolean contains(AnyType x) 
{ 
    return contains(x, root); 
} 

/** 
    * Make the tree logically empty. 
    */ 
public void makeEmpty() 
{ 
    root = null; 
} 

/** 
    * Test if the tree is logically empty. 
    * @return true if empty, false otherwise. 
    */ 
public boolean isEmpty() 
{ 
    return root == null; 
} 

/** 
    * Print the tree contents in sorted order. 
    */ 
public void printTree() 
{ 
    if(isEmpty()) 
     System.out.println("Empty tree"); 
    else 
     printTree(root); 
} 

/** 
    * Internal method to insert into a subtree. 
    * @param x the item to insert. 
    * @param t the node that roots the subtree. 
    * @return the new root of the subtree. 
*/ 
private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return new BinaryNode<>(x, null, null); 

    int compareResult = x.compareTo(t.element); 

    if(compareResult < 0) 
     t.left = insert(x, t.left); 
    else if(compareResult > 0) 
     t.right = insert(x, t.right); 
    else 
     ; // Duplicate; do nothing 
    return t; 
} 

/** 
    * Non recursive method, created by LR - 29-092014 

private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return new BinaryNode<>(x, null, null); 

    while (t != null) {  
     int compareResult = x.compareTo(t.element); 

     if(compareResult < 0) 
      t = t.left; 
     else if(compareResult > 0) 
      t = t.right; 
     else 
      ; // Duplicate; do nothing 
    } 
     return t; 
}*/ 

/** 
    * Internal method to remove from a subtree. 
    * @param x the item to remove. 
    * @param t the node that roots the subtree. 
    * @return the new root of the subtree. 
    */ 
private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return t; // Item not found; do nothing 

    int compareResult = x.compareTo(t.element); 

    if(compareResult < 0) 
     t.left = remove(x, t.left); 
    else if(compareResult > 0) 
     t.right = remove(x, t.right); 
    else if(t.left != null && t.right != null) // Two children 
    { 
     t.element = findMin(t.right).element; 
     t.right = remove(t.element, t.right); 
    } 
    else 
     t = (t.left != null) ? t.left : t.right; 
    return t; 
} 

/** 
    * Internal method to find the smallest item in a subtree. 
    * @param t the node that roots the subtree. 
    * @return node containing the smallest item. 
    */ 
private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return null; 
    else if(t.left == null) 
     return t; 
    return findMin(t.left); 
} 

/** 
    * Internal method to find the largest item in a subtree. 
    * @param t the node that roots the subtree. 
    * @return node containing the largest item. 
    */ 
private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t) 
{ 
    if(t != null) 
     while(t.right != null) 
      t = t.right; 

    return t; 
} 

/** 
    * Internal method to find an item in a subtree. 
    * @param x is item to search for. 
    * @param t the node that roots the subtree. 
    * @return node containing the matched item. 
    */ 
private boolean contains(AnyType x, BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return false; 

    int compareResult = x.compareTo(t.element); 

    if(compareResult < 0) 
     return contains(x, t.left); 
    else if(compareResult > 0) 
     return contains(x, t.right); 
    else 
     return true; // Match 
} 

/** 
    * Internal method to print a subtree in sorted order. 
    * @param t the node that roots the subtree. 
    */ 
private void printTree(BinaryNode<AnyType> t) 
{ 
    if(t != null) 
    { 
     printTree(t.left); 
     System.out.println(t.element); 
     printTree(t.right); 
    } 
} 

/** 
    * Internal method to compute height of a subtree. 
    * @param t the node that roots the subtree. 
    */ 
private int height(BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return -1; 
    else 
     return 1 + Math.max(height(t.left), height(t.right));  
} 

// Basic node stored in unbalanced binary search trees 
private static class BinaryNode<AnyType> 
{ 
     // Constructors 
    BinaryNode(AnyType theElement) 
    { 
     this(theElement, null, null); 
    } 

    BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt) 
    { 
     element = theElement; 
     left  = lt; 
     right = rt; 
    } 

    AnyType element;   // The data in the node 
    BinaryNode<AnyType> left; // Left child 
    BinaryNode<AnyType> right; // Right child 
} 


    /** The tree root. */ 
private BinaryNode<AnyType> root; 


} 

그리고 여기 내 주()

public static void main(String [ ] args) 
{ 
    BinarySearchTree<Integer> t = new BinarySearchTree<>(); 
    final int NUMS = 100000; // must be even 
    for(int i = 1; i <= NUMS; i++) 
    { 
     t.insert(i); 
    } 

} 

나는 다음과 같은 예외를 얻고있다

다음과 같은 방법에서 특히 굵은 선

private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) 
{ 
    if(t == null) 
     return new BinaryNode<>(x, null, null); 

    int compareResult = x.compareTo(t.element); 

    if(compareResult < 0) 
     t.left = insert(x, t.left); 
    else if(compareResult > 0) 
     **t.right = insert(x, t.right);** 
    else 
     ; // Duplicate; do nothing 
    return t; 
} 

그러나 불행히도이 같은 오류가 점점 오전에

Exception in thread "main" java.lang.StackOverflowError 

. 누군가 나에게 코드, 구성 또는 eclipse.ini 파일의 문제점을 알려주십시오.

+0

기본 힙 크기는 2GB (메모리의 1/4)이며 단순 테스트의 경우 값 당 20KB를 사용하는 경우 많습니다. 테스트를 변경하거나 코드를 수정하여 최대 메모리 사용량을 10 배 늘리기 전에 메모리를 많이 사용하지 않아도됩니다. –

+0

eclipse.ini 파일을 변경해야하며 -Xms1024 -Xmx2056 인수도 변경해야합니까? –

+0

메모리가 많을수록 프로그램을 실행하는 데 필요한 메모리가 줄어 듭니다. 나는 당신이 단지 8GB가 주어지고 일식의 시작이 아닌 당신의 프로그램을 구성함으로써 당신의 프로그램에 더 많은 메모리를 주었다면 1GB 만 제공하도록 유혹 될 것이다. 그러나이 경우에는 먼저 코드를 수정해야합니다. 1 백만 개의 항목이있는 트리 집합을 쉽게 만들 수 있어야합니다. –

답변

3

StackOverflowError은 메소드 재귀가 너무 깊음을 의미합니다. 이는 10k + 깊이와 같은 심도있는 재귀가 필요하거나 버그가 있음을 나타냅니다.

OutOfMemoryError으로 힙이 부족하면 힙 크기를 수정하거나 힙을 적게 사용하도록 프로그램을 수정하는 것이 좋습니다.

균형이 맞는 나무의 경우 깊이가 O (log2 (n))가되어야하지만 나무가 균형을 이루지 않습니다.

즉 당신의 나무가 효과적으로은 연결리스트로 설정되어있다이

1 \ 
    2 \ 
    3 \ 
     4 \ 
     5 \ 
      6 \ always more to the right. 

처럼 보이는, 그리고리스트의 원소의 개수는 스택이 하나 개 더 요소를 추가 갈 필요가 깊이입니다.

프로그램에서 이클립스가 아닌 -Xss으로 스택 깊이를 늘릴 수는 있지만 계획이 링크 된 목록이 아닌 트리를 구현하는 것이라면 균형 잡힌 트리로 만들 것을 제안합니다. 그렇지 않으면 재귀를 LinkedList로 사용하지 않는 것이 좋습니다 does)