Vector & memory fragmentation

Discussion in 'Java' started by Ike, Jul 28, 2005.

  1. Ike

    Ike Guest

    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
     
    Ike, Jul 28, 2005
    #1
    1. Advertising

  2. In article <8s8Ge.9718$>,
    "Ike" <> 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
     
    Steve W. Jackson, Jul 28, 2005
    #2
    1. Advertising

  3. Ike

    John Currier Guest

    Ike 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


    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
     
    John Currier, Jul 29, 2005
    #3
  4. Ike

    Ike Guest

    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

    "John Currier" <> wrote in message
    news:...
    > Ike 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

    >
    > 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
    >
     
    Ike, Jul 29, 2005
    #4
  5. Ike wrote:
    > 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

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jul 29, 2005
    #5
    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. DNass
    Replies:
    6
    Views:
    2,782
    Darryl L. Pierce
    Nov 28, 2004
  2. Tron Thomas

    Preventing memory fragmentation

    Tron Thomas, Dec 22, 2003, in forum: C++
    Replies:
    18
    Views:
    912
    Tom Plunket
    Jan 3, 2004
  3. spasmous
    Replies:
    5
    Views:
    2,430
  4. DLPnet

    Memory usage/Fragmentation

    DLPnet, Feb 16, 2005, in forum: C++
    Replies:
    5
    Views:
    427
    Ioannis Vranos
    Feb 16, 2005
  5. Replies:
    8
    Views:
    1,961
    Csaba
    Feb 18, 2006
Loading...

Share This Page