我想通过不断的添加链表来达到填满内存的效果,并且给出内存不足的提示,请问如何实现?
一下是我的代码:
////////////////////////////////////////////////////////////////
class LinkListApp
   {
   public static void main(String[] args)
   {
      LinkList theList = new LinkList();      boolean tof=true;
      int i=0,j=1,a;
      float b;
      while(tof)
      {
          a=(int)(Math.random()*(11));
          b=(float)((int)(Math.random()*(1000))/100);
          tof=theList.insertFirst(a,b);
          i++;j++;
          if(i==30000)
          {
              i=0;
              System.out.println("第"+ j +"次插入30000个Link");
          }
      }
   }
}
////////////////////////////////////////////////////////////////
class LinkList
   {
   private Link first;
// -------------------------------------------------------------
   public LinkList()
      {
      first = null; 
      }
// -------------------------------------------------------------
   public boolean insertFirst(int id, double dd)
      {
      Link newLink = new Link(id, dd);
      if(newLink.iData!=id && newLink.dData!=dd)
      {
          System.out.println("内存不足!");
          return false;
      }
      newLink.next = first;
      first = newLink;
      return true;
      }
// -------------------------------------------------------------
   }
////////////////////////////////////////////////////////////////
class Link
   {
   public int iData;
   public double dData;
   public Link next;
// -------------------------------------------------------------
   public Link(int id, double dd)
      {
      iData = id;
      dData = dd;
      }
    }
////////////////////////////////////////////////////////////////

解决方案 »

  1.   

    没做过测试,印象中如果内存不够的话,会抛出out of memory异常,所以你的那个判断可能走不到
      

  2.   

    内存溢出会抛出OutOfMemoryError,在Catch里进行处理。
      

  3.   


    try{
    }
    catch{
    }
    finally{
    }
    来捕获异常,在try中判断是否有异常,在catch中给出内存不足的提示信息
      

  4.   

    个人认为,在你的程序中,内存使用量不是很大,只是有个死循环。而且在JAVA 中是自动回收内存的。
    每次在insertFirst中new的类,前一次的JVM会自动回收。
    你可以用下面的语句查看JVM中利用的内存状况。
    Runtime.getRuntime().freeMemory()/(1024*1024);
      

  5.   


    这些语句不太会用,因为我是java入门不久
      

  6.   

    我的思路是想用那个死循环不断new链表实现填满内存的,不过因为if(newLink.iData!=id && newLink.dData!=dd) 一直都不会返回false,导致不能跳出循环。
    至于你给的代码是可以用来算剩下的内存,但是我是想通过塞满进去的链表的个数来测试内存大小。
    能不能在
    代码中返回false来令while循环停止
      

  7.   

    错误可以处理?我头一次听到啊!其实最简单byte[] b = new byte[Integer.MAX_VALUE]这样就可以抛出内存溢出错误
      

  8.   

    public static void main(String[] args) 
      { 
          LinkList theList = new LinkList(); 
          boolean tof=true; 
          int i=0,j=1,a; 
          float b; 
          try{
            while(tof){ 
              a=(int)(Math.random()*(11)); 
              b=(float)((int)(Math.random()*(1000))/100); 
              tof=theList.insertFirst(a,b); 
              i++;j++; 
              if(i==30000){ 
                  i=0; 
                  System.out.println("第"+ j +"次插入30000个Link"); 
              } 
            }
          }catch(OutOfMemoryError e){
            System.out.println("内存严重不足");
          }
      } 
      

  9.   

    用了你给的代码,运行了一下,在显示内存严重不足那句停了
    BlueJ下面有一段报错之类的:
    OutOfMemoryError:
    Java heap space (in java.nio.CharBuffer)仍需大侠指导!
      

  10.   

    个人认为,在你的程序中,内存使用量不是很大,只是有个死循环。而且在JAVA 中是自动回收内存的。 
    每次在insertFirst中new的类,前一次的JVM会自动回收。 
    你可以用下面的语句查看JVM中利用的内存状况。 
    Runtime.getRuntime().freeMemory()/(1024*1024);