zoukankan      html  css  js  c++  java
  • [讨论] String, StringBuilder, StringBuffer

    做了三个实验,之后我凌乱了


    使用String

    public class MemoryTest{
        public static void main(String args[]){
            System.out.print("String :");
            String s="abcdefghijklmnopqrstuvwxyz";
            System.out.print(" 当前虚拟机最大可用内存为 :");
            System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M");
            System.out.print(" 循环前,虚拟机已占用内存 :");
            System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
            int count = 0;
            while(true){
                try{ 
                    s+=s;
                    count++;
                    System.out.println("String 实 际 字 节数 :"+s.length()+"b");
                    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
                }
                catch(Error o){
                    System.out.println(" 循环次数 :"+count);
                    System.out.println("String 实 际 字 节数 :"+s.length()/1024/1024+"M");
                    System.out.print(" 循环后,已占用内存 :");
                    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
                    System.out.println("Catch 到的错误 :"+o);
                    break;
                }
            }
        }
    }
    E:\javacode\java_programmer_work>javac MemoryTest.java
    
    E:\javacode\java_programmer_work>java MemoryTest
    String : 当前虚拟机最大可用内存为 :247M
     循环前,虚拟机已占用内存 :15M
    String 实 际 字 节数 :52b
    15M
    String 实 际 字 节数 :104b
    15M
    String 实 际 字 节数 :208b
    15M
    String 实 际 字 节数 :416b
    15M
    String 实 际 字 节数 :832b
    15M
    String 实 际 字 节数 :1664b
    15M
    String 实 际 字 节数 :3328b
    15M
    String 实 际 字 节数 :6656b
    15M
    String 实 际 字 节数 :13312b
    15M
    String 实 际 字 节数 :26624b
    15M
    String 实 际 字 节数 :53248b
    15M
    String 实 际 字 节数 :106496b
    15M
    String 实 际 字 节数 :212992b
    15M
    String 实 际 字 节数 :425984b
    15M
    String 实 际 字 节数 :851968b
    15M
    String 实 际 字 节数 :1703936b
    15M
    String 实 际 字 节数 :3407872b
    25M
    String 实 际 字 节数 :6815744b
    50M
    String 实 际 字 节数 :13631488b
    100M
    String 实 际 字 节数 :27262976b
    201M
     循环次数 :20
    String 实 际 字 节数 :26M
     循环后,已占用内存 :247M
    Catch 到的错误 :java.lang.OutOfMemoryError: Java heap space
    

    使用StringBuilder(Java1.2之后推荐使用)

    public class MemoryTest1{
        public static void main(String args[]){
            System.out.print("StringBuilder :");
            StringBuilder s= new StringBuilder("abcdefghijklmnopqrstuvwxyz");
            System.out.print("当前虚拟机最大可用内存为 :");
            System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M");
            System.out.print("循环前,虚拟机已占用内存 :");
            System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
            int count = 0;
            while(true){
                try{ 
                    s.append(s);
                    count++;
                    System.out.println("String 实 际 字 节数 :"+s.length()+"b");
                    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
                }
                catch(Error o){
                    System.out.println("循环次数 :"+count);
                    System.out.println("String 实 际 字 节数 :"+s.length()/1024/1024+"M");
                    System.out.print("循环后,已占用内存 :");
                    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
                    System.out.println("Catch 到的错误 :"+o);
                    break;
                }
            }
        }
    }
    E:\javacode\java_programmer_work>javac MemoryTest1.java
    
    E:\javacode\java_programmer_work>java MemoryTest1
    StringBuilder :当前虚拟机最大可用内存为 :247M
    循环前,虚拟机已占用内存 :15M
    String 实 际 字 节数 :52b
    15M
    String 实 际 字 节数 :104b
    15M
    String 实 际 字 节数 :208b
    15M
    String 实 际 字 节数 :416b
    15M
    String 实 际 字 节数 :832b
    15M
    String 实 际 字 节数 :1664b
    15M
    String 实 际 字 节数 :3328b
    15M
    String 实 际 字 节数 :6656b
    15M
    String 实 际 字 节数 :13312b
    15M
    String 实 际 字 节数 :26624b
    15M
    String 实 际 字 节数 :53248b
    15M
    String 实 际 字 节数 :106496b
    15M
    String 实 际 字 节数 :212992b
    15M
    String 实 际 字 节数 :425984b
    15M
    String 实 际 字 节数 :851968b
    15M
    String 实 际 字 节数 :1703936b
    15M
    String 实 际 字 节数 :3407872b
    26M
    String 实 际 字 节数 :6815744b
    53M
    String 实 际 字 节数 :13631488b
    107M
    String 实 际 字 节数 :27262976b
    210M
    循环次数 :20
    String 实 际 字 节数 :26M
    循环后,已占用内存 :247M
    Catch 到的错误 :java.lang.OutOfMemoryError: Java heap space
    

    使用StringBuffer

    public class MemoryTest2{
        public static void main(String args[]){
            System.out.print("StringBuffer :");
            StringBuffer s= new StringBuffer("abcdefghijklmnopqrstuvwxyz");
            System.out.print("当前虚拟机最大可用内存为 :");
            System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M");
            System.out.print("循环前,虚拟机已占用内存 :");
            System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
            int count = 0;
            while(true){
                try{ 
                    s.append(s);
                    count++;
                  System.out.println("String 实 际 字 节数 :"+s.length()+"b");
                    System.out.println(+Runtime.getRuntime().totalMemory()/1024/1024+"Mb");
                }
                catch(Error o){
                    System.out.println("循环次数 :"+count);
                    System.out.println("String 实 际 字 节数 :"+s.length()/1024/1024+"M");
                    System.out.print("循环后,已占用内存 :");
                    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M");
                    System.out.println("Catch 到的错误 :"+o);
                    break;
                }
            }
        }
    }
    E:\javacode\java_programmer_work>javac MemoryTest2.java
    
    E:\javacode\java_programmer_work>java MemoryTest2
    StringBuffer :当前虚拟机最大可用内存为 :247M
    循环前,虚拟机已占用内存 :15M
    String 实 际 字 节数 :52b
    15Mb
    String 实 际 字 节数 :104b
    15Mb
    String 实 际 字 节数 :208b
    15Mb
    String 实 际 字 节数 :416b
    15Mb
    String 实 际 字 节数 :832b
    15Mb
    String 实 际 字 节数 :1664b
    15Mb
    String 实 际 字 节数 :3328b
    15Mb
    String 实 际 字 节数 :6656b
    15Mb
    String 实 际 字 节数 :13312b
    15Mb
    String 实 际 字 节数 :26624b
    15Mb
    String 实 际 字 节数 :53248b
    15Mb
    String 实 际 字 节数 :106496b
    15Mb
    String 实 际 字 节数 :212992b
    15Mb
    String 实 际 字 节数 :425984b
    15Mb
    String 实 际 字 节数 :851968b
    15Mb
    String 实 际 字 节数 :1703936b
    15Mb
    String 实 际 字 节数 :3407872b
    26Mb
    String 实 际 字 节数 :6815744b
    53Mb
    String 实 际 字 节数 :13631488b
    107Mb
    String 实 际 字 节数 :27262976b
    210Mb
    循环次数 :20
    String 实 际 字 节数 :26M
    循环后,已占用内存 :247M
    Catch 到的错误 :java.lang.OutOfMemoryError: Java heap space
    

    时间比较:  570,312,301

    从实验可以看出 传说之中的StringBuidler StringBuffer 居然比String还费内存,tell me why???

  • 相关阅读:
    learnyou 相关网站
    hdu 3038 How Many Answers Are Wrong
    hdu 3047 Zjnu Stadium 并查集高级应用
    poj 1703 Find them, Catch them
    poj 1182 食物链 (带关系的并查集)
    hdu 1233 还是畅通工程
    hdu 1325 Is It A Tree?
    hdu 1856 More is better
    hdu 1272 小希的迷宫
    POJ – 2524 Ubiquitous Religions
  • 原文地址:https://www.cnblogs.com/hzm112567/p/2765797.html
Copyright © 2011-2022 走看看