Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Memory works?

Status
Not open for further replies.

Tilen

Programmer
Apr 2, 2005
75
SI
Hi

I hope someone can clarify me this:

I'm processing some data. And I fill around 50 arrays with data. On my StatBar monitor I can see that after processing all data into arrays, I get around 900MB or RAM being used.

Then I free 3 biggest arrays, with

ArrayABC:=nil;

RAM drops down to 800Mb of being used, then I reload data into these 3 arrays and it comes up to 1010MB... WHY?

Shouldn't it just come back up to 900MB?

How does this Memory works?

Mabye ArraABC:=nil; is not the best thing to free space of that array?

Please advice,

Thanx a lot
tilen
 
Well..I'm not sure how to answer that...

I just use SetLength and fill in the data.

 
setlength(0) will deallocate the block.
it could be you're experiencing heap fragmentation...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Well it depends on how you are approaching it. You tell us very little of what and how you are doing it. Usually with pointer allocated memory, which is what you indicate in the first post, there's an explicit allocation and deallocation that occurs. If there is no deallocation you are getting a heap leak, and would cause what you are seeing, likely.

But the question is what you are doing to cause this.
 
OK.

Well,I read data from file and process it into arrays. At the begining I use SetLength with a startlength, ussually 1000 and then if processing gets over 1000 records I extend it again SetLength with 2000. When it's finished I count records and use SetLength again with total records number,1917.

That is how it comes to 900MB of RAM used.

Then I use Array:=nil for all three of then it drops down to 800MB. Then I process them again, but this time with just one SetLength, since I already know the records number.
And it comes up to over 1000MB.


How could I use pointers with arrays, to minimize memoryusage?
 
You are fragmenting the heap by doing what you're doing. That's why you are seeing what you are seeing.

Can't avoid it with variable length arrays.
 
Ok, so I guess resizing dynamic array causes inefficient memory usage, rigth?

Then why the first process, where I resize array until it finishes the data import, works better than the second process, where after array:=nil, I make just one SetLength(array,xy), since I already know the exact number of records and reload data from file?

Works better is meant as it doesn't take up as much space as the second process?
 
Delphi documentation points out that if you assign
"To deallocate a dynamic array, assign nil to a variable that references the array ... this disposes of the array, provided there are no other references to it"

So provided you are not keeping extra references of your tables It seems to me you are doing exactly the right thing. Except that unfortunately the rest of the world does not do exactly the right thing :).

Your problem i THINK lies in the bad memory management (this is true at least in Delphi 6). As "whosyourdaddy" says you are experiencing heap fragmentation.

One possible solution is to use alternative memory manager, its definitely worth a try:

Have a look at, it may help






"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Thanx for the info. I'll look into it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top