java - 64 位 Windows 10 中每个 java 进程的最大内存量?

标签 java eclipse windows jvm jvm-arguments

我使用的是 64 位 windows10 操作系统和 8GB RAM。我的 eclipse.ini 文件如下:

-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;


 }

这是我的 main()

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

}

我遇到了以下异常

Exception in thread "main" java.lang.StackOverflowError

在以下方法中,特别是粗线处

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;
 }

但不幸的是我遇到了同样的错误。有人请让我知道代码、配置或 eclipse.ini 文件有什么问题。

最佳答案

StackOverflowError 表示您的方法递归太深了。它表明您要么需要深度递归,例如 10k+ 深度,要么存在错误。

当您遇到 OutOfMemoryError 耗尽堆时,您可能会考虑修复堆大小,或修复程序以使用更少的堆。

在这种情况下,对于平衡树,您的深度应约为 O(log2(n)),但您的树并不平衡。

即你的树看起来像这样

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

实际上它已经变成了一个链表,链表中元素的数量就是堆栈需要再添加一个元素的深度。

您可以在程序(不是 eclipse)上使用 -Xss 增加堆栈深度,但是如果您的计划是实现树而不是链表,我建议您将其设为平衡树(或者避免像 LinkedList 那样使用递归)

关于java - 64 位 Windows 10 中每个 java 进程的最大内存量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39814795/

相关文章:

java - 如何在 JUnit 测试中强制命中二级缓存?

eclipse - STS(Eclipse)maven添加远程存储库

java - 我在 Eclipse 中看不到 Android SDK javadoc

.net - C# CultureInfo.GetCultures 返回一个(几乎)空列表

java - javafx 中的 java.lang.reflect.InitationTargetException 中的异常

java - 如何使用 rjava 在 R 中调用 String[][]

c++ - VS 2013 OpenCV错误: Cannot find or open the PDB file

java - 如何在 Windows Server 2008 上将 Java .jar 文件作为 Windows 服务运行?

java - 为什么在这种情况下删除通用类型会阻止覆盖?

java - 是否可以为 Java 中的 C 程序打印诊断信息?