Link to home
Start Free TrialLog in
Avatar of alexiat
alexiat

asked on

Record vs Class Type

I don't think this is a difficult question, but I'd like to understand this so I don't continue down a wrong path.

I understand that the difference between using a Record vs. a Class Type variable is that the record type uses local memory vs an object being allocated in dynamic memory.  My app uses a lot of memory so I am trying to use it as efficiently as I can.

I know there are a lot of variables involved here, but is there some kind of "basic" record size that is a kind of cutoff for "that should always be an object not a record" sort of thing?  Most sources I have found when researching this say something like "use records for small and simple data structures" but I'm not sure what "small" means.

I use a lot of records that are under 2k in size and a few up to 5k in size.  I realize this got a little wordy for a simple question.  Can anyone give me some guidance here?

Thanks.
Avatar of MerijnB
MerijnB
Flag of Netherlands image

first of all:

a Record vs. a Class Type variable is that the record type uses local memory vs an object being allocated in dynamic memory.

this is not true. Both use the same memory 'pool'. The difference is that a record is just uses a pile of memory, while an object is actually a pointer to the used memory. This means practically that if you have a procedure of function, and you pass a record to it, all the memory used by the record has to be copied, while if you pass an object, only the pointer (4 bytes) have to be copied.

There is no real way of determining when a record is better. Most of the times an object has actually more overhead then a record. On the other hand, when you have for example an array of records vs an array of objects, the array of records might pose a problem due too memory fragmentation.

Back to your problem: you say that your application uses a lot of memory. How much is a lot? Do you know what this memory is used for at the moment. A bunch of records / objects. What version of Delphi are you using, it could very well be a case of memory fragmentation.

5k for a a single record is quite a lot, what is in there?
Avatar of alexiat
alexiat

ASKER

Delphi 2007 on Vista or XP.

The main app has a number of different, complex windows (holding string grids, graphs, tabsets etc). I do create and free each window as needed.  We have our own proprietary "database" (i.e. it isn't a .db file etc) of information the user uses and it requires a lot of loading/unloading of memory to get/release this information.   User information for these various windows is stored in different files.  Because the information can be complex, or because there is a lot of information but I only need small parts of it in various areas, it is stored in files and read into records.  In other places, I need to store up to 200 different real values for quick access.  

I know the above is a bit broad in terms but does it help?


200 reals is 'only' 1600 bytes, which is a lot less then 2k .. 5k.
How many of there records are kept in memory?
Avatar of alexiat

ASKER

The 200 reals are part of a record which contains a few more items such as string[255], integers, booleans and words.  Right around 2400 bytes for one particular record.  There is one record which holds 2 of these arrays of reals plus others; it is around 3k (not 5k - sorry, mistyped).  A number of records less than 1k.  Up to 3 records in memory.



so in worst case (3 records of each 6k) you have a whopping total of 6kB used by the records.
I think this is not the place to save memory :)
sorry, 3 records of 3k, so total of 9 kB
Avatar of alexiat

ASKER

I really was looking for guidance as to what was more efficient since information is swapped in and out a lot, rather than if these 3 specific records would cause a memory problem.   This is a large application with a lot more to come than what I have now.

I guess you answered my question in a way when you said "There is no real way of determining when a record is better."   So, I guess I'll just move forward as if I knew what I was doing.  ;)

Thanks.

ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alexiat

ASKER

Thanks.  That explanation helps a lot.