Re: Vector & memory fragmentation
- From: "Steve W. Jackson" <stevewjackson@xxxxxxxxxxx>
- Date: Thu, 28 Jul 2005 12:46:10 -0500
In article <8s8Ge.9718$dU3.4334@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"Ike" <rxv@xxxxxxxxxxx> wrote:
> 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 =
--
Steve W. Jackson
Montgomery, Alabama
.
- References:
- Vector & memory fragmentation
- From: Ike
- Vector & memory fragmentation
- Prev by Date: Re: XPath on DOMTree
- Next by Date: Re: J2EE installation help
- Previous by thread: Vector & memory fragmentation
- Next by thread: Re: Vector & memory fragmentation
- Index(es):
Relevant Pages
|