Re: Vector & memory fragmentation



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
.



Relevant Pages

  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: High Memory Consumption of Classes and Arrays
    ... Only the array itself has overhead. ... memory as a reference type. ... > least consume 40 bytes of memory. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Fast linked list
    ... > amounts of memory rather than huge chunks of it. ... random insertions into a vector/dynamic array are not as slow ... to cause a cache miss. ... some hard numbers on speed differences between lists and arrays. ...
    (microsoft.public.vc.mfc)
  • Re: Fast linked list
    ... > amounts of memory rather than huge chunks of it. ... random insertions into a vector/dynamic array are not as slow ... to cause a cache miss. ... some hard numbers on speed differences between lists and arrays. ...
    (microsoft.public.vc.language)
  • Re: not enough storage... error using GetRows
    ... > array only contained ~150,000 rows. ... It took 19 minutes for GetRows to return (db ... and the prog had consumed 200MB of memory. ... The first one allocates some 180MB, the next two only allocate about 47MB ...
    (microsoft.public.vb.database.ado)