Vector takes a lot memory

H

homecurr

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;
}
 
M

Michael Borgwardt

homecurr said:
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).
 
A

Andrew Thompson

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
 
T

Thomas G. Marshall

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.
 
T

Thomas G. Marshall

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.
 
T

Thomas G. Marshall

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top