Vector & memory fragmentation

I

Ike

If I have a Vevtor:

Vector vector = new Vector();

and I am tracking a particular Object within that Vector, whereby I obtain
it;s original position within the Vector, say, int u. Then say, through
various Vector operations, the location of that particular Object is no
longer at u; Therefore, I periodically want to reset it back to being in
position u within the Vector.

Presently, I do this with :

vector.removeElement(myObject);
vector.insertElementAt(myObject, u);

But I am not so certain this is the "best" way to do this. My concern is
memory fragmentation, or, more generally, the ability for such a procedure
to run for days on end without this developing into a problem somehow.

Given that, is there a better way to accomplish what I am doing here?
Thanks, Ike
 
S

Steve W. Jackson

Ike said:
If I have a Vevtor:

Vector vector = new Vector();

and I am tracking a particular Object within that Vector, whereby I obtain
it;s original position within the Vector, say, int u. Then say, through
various Vector operations, the location of that particular Object is no
longer at u; Therefore, I periodically want to reset it back to being in
position u within the Vector.

Presently, I do this with :

vector.removeElement(myObject);
vector.insertElementAt(myObject, u);

But I am not so certain this is the "best" way to do this. My concern is
memory fragmentation, or, more generally, the ability for such a procedure
to run for days on end without this developing into a problem somehow.

Given that, is there a better way to accomplish what I am doing here?
Thanks, Ike

First and foremost, I recommend discarding any notions about memory
fragmentation. Modern systems manage memory better than you or I could
likely do in our own code. Where things live in memory isn't usually
something you should care about. After all, what's really in the
Vector's elements is a collection of address references to where the
referenced objects are located -- numbers. My two cents...

To get a good answer to your question about tracking an item in a
Vector, you might consider looking at the source code. Sun's JDK
includes src.zip. Vector actually keeps its items in an array. Only
when you remove something in the middle could any item "below" change
its index. See the code for removeElementAt and you'll see that it
doesn't remove the item at all (removeElement calls removeElementAt).
Instead, it copies all higher-indexed elements upward in the array so
that they move up by one, then sets to null the now-duplicate item at
the end. Inserting an item copies the entire array's contents below the
new index downward by one, then places the value you provide in the
appropriate spot.

So what you've effectively done with those two lines is two array copy
actions. Is it worth that? I would venture that you might actually be
better off simply keeping up with the item's location rather than moving
it from time to time. But you should find answers in the code.

HTH.

= Steve =
 
J

John Currier

Ike said:
If I have a Vevtor:

Vector vector = new Vector();

and I am tracking a particular Object within that Vector, whereby I obtain
it;s original position within the Vector, say, int u. Then say, through
various Vector operations, the location of that particular Object is no
longer at u; Therefore, I periodically want to reset it back to being in
position u within the Vector.

Presently, I do this with :

vector.removeElement(myObject);
vector.insertElementAt(myObject, u);

But I am not so certain this is the "best" way to do this. My concern is
memory fragmentation, or, more generally, the ability for such a procedure
to run for days on end without this developing into a problem somehow.

Given that, is there a better way to accomplish what I am doing here?
Thanks, Ike

What problem are you really trying to solve by tracking the object
within the vector? We may have a more elegant approach for you.

John
http://schemaspy.sourceforge.net
 
I

Ike

I have a queue class, that uses a Vector. When the 0th element is "popped,"
it is remove() from the 0'th element, and add() back to the Vector (thus, at
the end element).

Some elements need to be "frozen" in their position. -Ike
 
T

Thomas Weidenfeller

Ike said:
I have a queue class, that uses a Vector. When the 0th element is "popped,"
it is remove() from the 0'th element, and add() back to the Vector (thus, at
the end element).

First, don't top-post (if you don't know what this is, goolge it).

Regarding your usage of a Vector as queue: Since 1.5 Java comes with a
bunch of Queue classes, and LinkedList was refitted to the Queue interface.

Before 1.5 LinkedList would also have been my candidate for a simple
queue like you describe. It would avoid the heavy array copying that
Vector has to do when you remove the first element. I would not worry
about memory fragmentation when using Vector, but the time wasted due to
the array copying.

/Thomas
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top