Java不同数据结构内存消耗比较

来源:互联网 发布:阿里云服务器受到ddos 编辑:程序博客网 时间:2024/05/22 15:22

测试不同存储方式对内存的消耗情况:

<span style="font-size:14px;">package bfs;import java.util.HashSet;import java.util.TreeMap;public class MemStatistic {public static void main(String[] args) {//BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();Runtime rt = Runtime.getRuntime();    System.out.println("Total Memory = " + rt.totalMemory() + " Used Memory = " + (rt.totalMemory() - rt.freeMemory()));    int verticesSize = 5000000 ;     /**   * 数组部分内存开销测试   */    Vertex  vertices[]  = new Vertex[verticesSize] ;    int[] edges = new int[verticesSize] ;    boolean[] visited = new boolean[verticesSize];    for(int i = 0 ; i<verticesSize ; i++){    vertices[i] = new Vertex(i,i + (i*3)%100, 1) ;    }        for(int j =0 ;j<verticesSize ; j++){    edges[j] = j;    visited[j] = true ;    }
<span style="white-space:pre"></span>  /**<span style="white-space:pre"></span>   * 链表部分内存开销测试<span style="white-space:pre"></span>   */    <span style="white-space:pre"></span>    ArrayList<Vertex>  verticesList  =  new ArrayList<Vertex>() ;<span style="white-space:pre"></span>    int[] edges = new int[verticesSize] ;<span style="white-space:pre"></span>    boolean[] visited = new boolean[verticesSize];<span style="white-space:pre"></span>    for(int i = 0 ; i<verticesSize ; i++){<span style="white-space:pre"></span>    <span style="white-space:pre"></span>verticesList.add(new Vertex(i,i + (i*3)%100, 1)) ;<span style="white-space:pre"></span>    }       /**   * TreeMap部分内存开销测试     *///    TreeMap<Integer, HashSet<Integer>>  vertices2 = new TreeMap<Integer, HashSet<Integer>>() ;//    for(int i = 0 ; i<verticesSize ; i++){//    HashSet<Integer> values = new HashSet<Integer>() ;//    values.add(i + (i*3)%100) ;//    vertices2.put(i, values) ;   //    }      /**   * TreeMap部分内存开销测试     *///    TreeMap<Integer, String>  vertices3 = new TreeMap<Integer, String>() ;//    for(int i = 0 ; i<verticesSize ; i++){//    Integer r = i + (i*3)%100  ;//    String values =r.toString() ;//    vertices3.put(i, values) ;   //    }         System.out.println("Total Memory = " + rt.totalMemory() + " Used Memory = " + (rt.totalMemory() - rt.freeMemory()));}    }class Vertex{public int id ; //顶点值public int start ;  //edges数组中边的起始位置public int edgeNo;   //该顶点的边数目public Vertex( int iD, int x, int y){ id = iD; start = x ; edgeNo = y ;}public String toString(){        return new String(id+":"+start+"-"+edgeNo) ;}}</span>

结果1,2,3分别是:

500万个点:

Total Memory = 127991808 Used Memory = 669888
Total Memory = 399310848 Used Memory = 165807688  约165M


Total Memory = 127991808 Used Memory = 669888
Total Memory = 1809711104 Used Memory = 1283182952 约1.28G


Total Memory = 127991808 Used Memory = 669888
Total Memory = 842203136 Used Memory = 562868512   约562M   


 第一种消耗的内存只有165M, 真少啊 !!!!  链表的开销略高于它,约为186M.


此外,发现TreeMap比HashMap消耗的内存略少些,好东西啊!


下面是之前做过的测试: 1000万条边,约160M大小文件。

使用TreeMap<Integer, HashSet<Integer>>存储,结果

使用eclipse Memeory Analyzer分析,Map占了大部分内存,约1G空间:



0 0
原创粉丝点击