Java变量作用域内存

我一直在想一个问题,做循环的时候变量在循环体内定义省内存还是变量在循环体外定义省内存,于是做了下实验:
import junit.framework.TestCase;

class HoldMemory{
	String str01="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	
	public HoldMemory(){
	}
}

public class MemoryCase extends TestCase{

	public void testname() throws Exception {
		for (int i = 0; i < 50000; i++) {
			HoldMemory holdMemory=new HoldMemory();
		}
		System.out.println(Runtime.getRuntime().freeMemory());
		
	}
}


和下面的code进行比较:
import junit.framework.TestCase;

class HoldMemory{
	String str01="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	
	public HoldMemory(){
	}
}

public class MemoryCase extends TestCase{

	public void testname() throws Exception {
                HoldMemory holdMemory=null;
		for (int i = 0; i < 50000; i++) {
		     holdMemory=new HoldMemory();
		}
		System.out.println(Runtime.getRuntime().freeMemory());
		
	}
}

结果两个打印的结果是一样的,当然我是分两次运行的,不是在一一起运行的。所以感觉没有区别,可能从作用域来说作用域越小,越容易被回收吧,所以并不是变量定义在外面科学,感觉应该是定义在里面更好一些,因为生命周期短,容易被回收。
但是从CPU角度讲,可能写在外面的话,创建新变量的过程省了,是不是更加节省CPU执行单元?

另外有以下代码可以跑着看看内存什么时候被回收,你可以试试写在外面好还是写在里面好,我跑着试了试,感觉都一样:
package com.ibm.partnerworld.vic.testcase;

import junit.framework.TestCase;

class HoldMemory{
	String str01="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str02="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str03="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str04="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str05="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str06="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str07="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str08="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str09="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str10="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str11="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str12="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str13="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str14="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	String str15="12111111111111111111111111111111111111111111111111111111111111111111111111111111111";
	
	public HoldMemory(){
		try {
			System.out.println("Created");
			Thread.currentThread().sleep(3);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		System.out.println("I was reused!");
	}
}

public class MemoryCase extends TestCase{

	public void testname() throws Exception {
		HoldMemory holdMemory=null;
		for (int i = 0; i < 50000; i++) {
			holdMemory=new HoldMemory();
			System.out.println("total=>"+Runtime.getRuntime().totalMemory());
			System.out.println("free=>"+Runtime.getRuntime().freeMemory());
		}
		System.out.println(Runtime.getRuntime().freeMemory());
		
	}
}

你可能感兴趣的:(java,JUnit,单元测试,IBM,Netbeans)