前些天看了篇文章:
[url]
http://www.ibm.com/developerworks/cn/java/j-perf08273/index.html[/url]
里面关于static的说法,我对起有了较深一步的研究
测试代码:
public class bigObject {
int a=9;
long b=12;
float c=12.3f;
int [] cc=new int [8111110];
}
//测试类
public class tests {
private static Object bigObject; //(1)
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
long numObjects = 0;
// Object bigObject; //(2)
while (true) {
// bigObject = null; //(3)
bigObject = new bigObject ();
long endTime = System.currentTimeMillis();
++numObjects; // We print stats for every two seconds
System.out.println("Objects created = "+ numObjects );(4)
//我在这里加上(4)主要是想看看程序到底在溢出的时候其能建立几个对象
if (endTime - startTime >= 2000) {
System.out.println("Objects created per 2 seconds = "+ numObjects );
startTime = endTime;
numObjects = 0;
}
}
}
}
在做这个测试的时候,需要将自己的jvm内部的堆大小(要不然看到的效果不明显),在eclipse中将vm args 调整为-Xms12m -Xms12m
(注明:下面的所有测试都是在目前上面的1,2,3,4状态修改下测试)
现在测试:
运行tests
你会发现:
Objects created = 1
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
如果你将(2)处的注释去掉,运行结果如下:
Objects created = 1
Objects created = 2
Objects created = 3
Objects created = 4
Objects created = 5
Objects created = 6
。。。。
Objects created per 2 seconds = 95
。。。。。
一直运行下去
如果我们将(1)注释掉,将(2)注释去掉,我们会发现其运行结果
Objects created = 1
Objects created = 2
Objects created = 3
Objects created = 4
Objects created = 5
Objects created = 6
。。。。
Objects created per 2 seconds = 95
。。。。。
一直运行下去
发现和上面的是一样。
如果我们将(1)注释掉,将(2)、(3)注释去掉,我们会发现其运行结果也和上面一样
好了我做如下的总结:
静态变量: 先建立内存对象,再将引用指过去【所以如果引用不申明为null的话,其实就好比在第二次引用指过去的时候(bigObject = new bigObject ();),其已经在内存中有一个原来的引用对象,只是在这次对象建立之后,再将原来的引用改过来】但是如果内存空间只能够容忍一个对象怎么办?(垃圾回收机制不会去回收第一次建立的对象,因为其引用还存在的)那么就会出现java.lang.OutOfMemoryError: Java heap space,
但是如果将上例中的private static Object bigObject; 申明为 Object bigObject; 那样的话,在执行的过程中是先建立引用再指向对象,所以在上例中,将bigobject 设置为null或者不设置时没有影响的,因为原来的第一个引用已经自动的消失了【在执行bigObject = new bigObject ();的时候,先消去原来的引用(垃圾回收机制在知道当前内存不够的时候,自动的回去回收第一次建立的对象,因为其引用已经消失了),再建立对象,再将引用指向现在的对象】。
[url]
http://www.ibm.com/developerworks/cn/java/j-perf08273/index.html[/url]
里面关于static的说法,我对起有了较深一步的研究
测试代码:
public class bigObject {
int a=9;
long b=12;
float c=12.3f;
int [] cc=new int [8111110];
}
//测试类
public class tests {
private static Object bigObject; //(1)
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
long numObjects = 0;
// Object bigObject; //(2)
while (true) {
// bigObject = null; //(3)
bigObject = new bigObject ();
long endTime = System.currentTimeMillis();
++numObjects; // We print stats for every two seconds
System.out.println("Objects created = "+ numObjects );(4)
//我在这里加上(4)主要是想看看程序到底在溢出的时候其能建立几个对象
if (endTime - startTime >= 2000) {
System.out.println("Objects created per 2 seconds = "+ numObjects );
startTime = endTime;
numObjects = 0;
}
}
}
}
在做这个测试的时候,需要将自己的jvm内部的堆大小(要不然看到的效果不明显),在eclipse中将vm args 调整为-Xms12m -Xms12m
(注明:下面的所有测试都是在目前上面的1,2,3,4状态修改下测试)
现在测试:
运行tests
你会发现:
Objects created = 1
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
如果你将(2)处的注释去掉,运行结果如下:
Objects created = 1
Objects created = 2
Objects created = 3
Objects created = 4
Objects created = 5
Objects created = 6
。。。。
Objects created per 2 seconds = 95
。。。。。
一直运行下去
如果我们将(1)注释掉,将(2)注释去掉,我们会发现其运行结果
Objects created = 1
Objects created = 2
Objects created = 3
Objects created = 4
Objects created = 5
Objects created = 6
。。。。
Objects created per 2 seconds = 95
。。。。。
一直运行下去
发现和上面的是一样。
如果我们将(1)注释掉,将(2)、(3)注释去掉,我们会发现其运行结果也和上面一样
好了我做如下的总结:
静态变量: 先建立内存对象,再将引用指过去【所以如果引用不申明为null的话,其实就好比在第二次引用指过去的时候(bigObject = new bigObject ();),其已经在内存中有一个原来的引用对象,只是在这次对象建立之后,再将原来的引用改过来】但是如果内存空间只能够容忍一个对象怎么办?(垃圾回收机制不会去回收第一次建立的对象,因为其引用还存在的)那么就会出现java.lang.OutOfMemoryError: Java heap space,
但是如果将上例中的private static Object bigObject; 申明为 Object bigObject; 那样的话,在执行的过程中是先建立引用再指向对象,所以在上例中,将bigobject 设置为null或者不设置时没有影响的,因为原来的第一个引用已经自动的消失了【在执行bigObject = new bigObject ();的时候,先消去原来的引用(垃圾回收机制在知道当前内存不够的时候,自动的回去回收第一次建立的对象,因为其引用已经消失了),再建立对象,再将引用指向现在的对象】。