Vector takes a lot memory

Discussion in 'Java' started by homecurr, Nov 25, 2004.

  1. homecurr

    homecurr Guest

    Here is my code.

    The String take about 20000 * 30 * 8 = 5M. But once I put them in the
    Vector, it takes about 100M memory. Why? How can I fix it? Another
    good data structure than Vector?

    Thanks,

    qq


    public static void main(String[] args) {
    Vector data = new VTest().load();
    int i = 1;
    }

    public Vector load(){
    int row = 20000;
    int col = 30;
    Vector data = new Vector(100, 100);
    for (int i = 0; i < row; i++){
    String[] line = new String[col];
    for (int j = 0; j < col; j++){
    line[j] = " " + (int)(Math.random() * 100000000);
    }
    data.add(line);
    }
    return data;
    }
     
    homecurr, Nov 25, 2004
    #1
    1. Advertising

  2. homecurr wrote:

    > Here is my code.
    >
    > The String take about 20000 * 30 * 8 = 5M.


    Nope. Each of your 20000*30 Strings takes about 56 bytes, not 8, since Java uses
    unicode and there is an overhead for each object - twice, because String encapsulates
    a char[] array, the length field of the array and probably some additional overhead.
    That alone makes for 32M. Then there are 20000 String arrays of length 30, which need
    about 136 bytes for the array overhead and 30 references - another 2.6M.

    If you don't believe my numbers, read this:
    http://www.javaworld.com/javaworld/javatips/jw-javatip130.html

    > But once I put them in the
    > Vector, it takes about 100M memory.


    The vector itself should add only about 100k overhead. What exactly "takes about 100M
    memory"? The JVM process? That contains a lot more than just your vector,and always
    keeps a lot of empty space ready if possible.

    > Why? How can I fix it?


    How about not saving numbers as Strings? That's your biggest waste of space by far.

    > Another good data structure than Vector?


    Your problem is not with Vector, though it shouldnÄt be used anymore (instead, ArrayList).
     
    Michael Borgwardt, Nov 25, 2004
    #2
    1. Advertising

  3. On 25 Nov 2004 08:21:33 -0800, homecurr wrote:

    > public Vector load(){
    > int row = 20000;
    > int col = 30;
    > Vector data = new Vector(100, 100);
    > for (int i = 0; i < row; i++){


    If it is always a specific size, I would recommend using an
    array rather than a vector. This will allow you to store
    primitve types rather than String objects.

    > String[] line = new String[col];
    > for (int j = 0; j < col; j++){
    > line[j] = " " + (int)(Math.random() * 100000000);


    What is the point of the " "? It would be much more efficient to
    keep this as 'int' inside the array (OK, assuming you toss the
    vector, a multi-dinensional array)

    > }
    >
    > data.add(line);
    > }
    > return data;
    > }


    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Nov 25, 2004
    #3
  4. Michael Borgwardt coughed up:
    > homecurr wrote:


    ....[rip]...


    >> Another good data structure than Vector?

    >
    > Your problem is not with Vector, though it shouldnÄt be used anymore
    > (instead, ArrayList).


    In general, switching from "legacy" classes to the collections framework is
    a good idea, but you need to be aware that you will be unable to extend a
    synchronized version of ArrayList. You can only grab a synchronized version
    of ArrayList at runtime, which means that if you wish to extend ArrayList
    /and/ have it synchronized, you must override every method with a
    synchronized one.


    --
    "Gentlemen, you can't fight in here! This is the War Room!"
     
    Thomas G. Marshall, Nov 26, 2004
    #4
  5. Michael Borgwardt coughed up:
    > homecurr wrote:


    ....[rip]...


    >> Another good data structure than Vector?

    >
    > Your problem is not with Vector, though it shouldnÄt be used anymore
    > (instead, ArrayList).


    In general, switching from "legacy" classes to the collections framework is
    a good idea, but you need to be aware that you will be unable to extend a
    synchronized version of ArrayList. You can only grab a synchronized version
    of ArrayList at runtime, which means that if you wish to extend ArrayList
    /and/ have it synchronized, you must override every method with a
    synchronized one.


    --
    "Gentlemen, you can't fight in here! This is the War Room!"
     
    Thomas G. Marshall, Nov 26, 2004
    #5
  6. Thomas G. Marshall coughed up:
    > Michael Borgwardt coughed up:
    >> homecurr wrote:

    >
    > ...[rip]...
    >
    >
    >>> Another good data structure than Vector?

    >>
    >> Your problem is not with Vector, though it shouldnÄt be used anymore
    >> (instead, ArrayList).

    >
    > In general, switching from "legacy" classes to the collections
    > framework is a good idea, but you need to be aware that you will be
    > unable to extend a synchronized version of ArrayList. You can only
    > grab a synchronized version of ArrayList at runtime, which means that
    > if you wish to extend ArrayList /and/ have it synchronized, you must
    > override every method with a synchronized one.



    Sorry for the Deja Vu feeling you're experiencing. This is because of
    Verizon's @#$%ed up NNTP servers of late fooling me into thinking that the
    message was not posted and requiring a repost. I'm very sorry.


    --
    Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....
     
    Thomas G. Marshall, Nov 26, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary
    Replies:
    1
    Views:
    325
    Janaka
    Oct 16, 2003
  2. =?Utf-8?B?UmFuIERhdmlkb3ZpdHo=?=

    aspnet_wp takes a lot of memory even after appdomain restarted

    =?Utf-8?B?UmFuIERhdmlkb3ZpdHo=?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    409
    =?Utf-8?B?UmFuIERhdmlkb3ZpdHo=?=
    Mar 29, 2005
  3. Raga
    Replies:
    4
    Views:
    1,693
  4. Replies:
    8
    Views:
    1,987
    Csaba
    Feb 18, 2006
  5. RJ
    Replies:
    5
    Views:
    134
    Peter J. Holzer
    Feb 19, 2007
Loading...

Share This Page