java如何提示内存不足

RacheAngel 2008-11-23 04:58:19
我想通过不断的添加链表来达到填满内存的效果,并且给出内存不足的提示,请问如何实现?
一下是我的代码:
////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////
...全文
294 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiyuzhongxin789 2008-11-24
  • 打赏
  • 举报
回复
个人认为,在你的程序中,内存使用量不是很大,只是有个死循环。而且在JAVA 中是自动回收内存的。
每次在insertFirst中new的类,前一次的JVM会自动回收。
你可以用下面的语句查看JVM中利用的内存状况。
Runtime.getRuntime().freeMemory()/(1024*1024);
RacheAngel 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 preferme 的回复:]
Java codepublic 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){

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

仍需大侠指导!
liuzhengkang 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chenweionline 的回复:]
内存溢出会抛出OutOfMemoryError,在Catch里进行处理。
[/Quote]
顶!!!
bigbro001 2008-11-23
  • 打赏
  • 举报
回复
等待高人解释
冰思雨 2008-11-23
  • 打赏
  • 举报
回复
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("内存严重不足");
}
}
Looking_Glass 2008-11-23
  • 打赏
  • 举报
回复
错误可以处理?我头一次听到啊!其实最简单byte[] b = new byte[Integer.MAX_VALUE]这样就可以抛出内存溢出错误
RacheAngel 2008-11-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gushidiyi 的回复:]
个人认为,在你的程序中,内存使用量不是很大,只是有个死循环。而且在JAVA 中是自动回收内存的。
每次在insertFirst中new的类,前一次的JVM会自动回收。
你可以用下面的语句查看JVM中利用的内存状况。
Runtime.getRuntime().freeMemory()/(1024*1024);
[/Quote]
我的思路是想用那个死循环不断new链表实现填满内存的,不过因为if(newLink.iData!=id && newLink.dData!=dd) 一直都不会返回false,导致不能跳出循环。
至于你给的代码是可以用来算剩下的内存,但是我是想通过塞满进去的链表的个数来测试内存大小。
能不能在
[Quote]
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;
}
[/Quote]
代码中返回false来令while循环停止
RacheAngel 2008-11-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 shenjie1989 的回复:]

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

这些语句不太会用,因为我是java入门不久
傲五洲 2008-11-23
  • 打赏
  • 举报
回复
个人认为,在你的程序中,内存使用量不是很大,只是有个死循环。而且在JAVA 中是自动回收内存的。
每次在insertFirst中new的类,前一次的JVM会自动回收。
你可以用下面的语句查看JVM中利用的内存状况。
Runtime.getRuntime().freeMemory()/(1024*1024);
shenjie1989 2008-11-23
  • 打赏
  • 举报
回复

try{
}
catch{
}
finally{
}
来捕获异常,在try中判断是否有异常,在catch中给出内存不足的提示信息
jxxx2967 2008-11-23
  • 打赏
  • 举报
回复
学习
chenweionline 2008-11-23
  • 打赏
  • 举报
回复
内存溢出会抛出OutOfMemoryError,在Catch里进行处理。
RacheAngel 2008-11-23
  • 打赏
  • 举报
回复
忽略那判断,可不可以用什么其他的方法显示内存不足啊?
fanyamin 2008-11-23
  • 打赏
  • 举报
回复
没做过测试,印象中如果内存不够的话,会抛出out of memory异常,所以你的那个判断可能走不到
Successman2013 2008-11-23
  • 打赏
  • 举报
回复
关注中!!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧