我想通过不断的添加链表来达到填满内存的效果,并且给出内存不足的提示,请问如何实现?
一下是我的代码:
////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////
一下是我的代码:
////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////
解决方案 »
- 一个排序问题
- socket聊天室,功能需实现群聊和私聊
- JEditorPane中使用javascript
- 一个出错问题!!!急急急急急急!!!
- 下拉列表JComboBox相关问题
- 例外处理例子学习时碰到的问题
- 我用new String(),可是系统会报出错.怎么回事???
- 谁能推荐一个从已有的JAVA 代码生成其对应UML class图的工具?
- 各位大侠,一般我们做了一个application,加那些代码使这个application的窗口大小不能改变。(内空)给25分
- 请高手帮我编译一下这个文件,50分相谢!!
- spring mvc 使用MultiActionController的问题
- 调用!
try{
}
catch{
}
finally{
}
来捕获异常,在try中判断是否有异常,在catch中给出内存不足的提示信息
每次在insertFirst中new的类,前一次的JVM会自动回收。
你可以用下面的语句查看JVM中利用的内存状况。
Runtime.getRuntime().freeMemory()/(1024*1024);
这些语句不太会用,因为我是java入门不久
至于你给的代码是可以用来算剩下的内存,但是我是想通过塞满进去的链表的个数来测试内存大小。
能不能在
代码中返回false来令while循环停止
{
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("内存严重不足");
}
}
BlueJ下面有一段报错之类的:
OutOfMemoryError:
Java heap space (in java.nio.CharBuffer)仍需大侠指导!
每次在insertFirst中new的类,前一次的JVM会自动回收。
你可以用下面的语句查看JVM中利用的内存状况。
Runtime.getRuntime().freeMemory()/(1024*1024);