What is the array of objects memory

Arrays and storage

An array, unlike data that you store in tables and records on disk, is always kept entirely in memory.

For example, if you enter all postcodes in a table [Postcode], it will contain around 10,000 data records. The table naturally contains other data fields such as country code and city. If you now select postcode area 8, the 4D database engine creates the corresponding data record selection in the [Postcode] table and loads the data records only when required, e.g. for display on the screen or for printing. In other words, you are working with an ordered series of values ​​of the same type that the engine loads partially from disk into memory.

This approach is unthinkable for arrays. This has the following reasons:

  • To manage the three pieces of information, country code, zip code, and city, you would need to keep three large arrays in memory.
  • Since an array is always kept entirely in memory, you would have to keep all of the information from these arrays in memory for the entire working session, even if you don't need the data all the time.
  • Each time the database is started or stopped, these arrays would have to be completely loaded and then saved on the hard disk, even if the data was neither used nor changed during the entire work session.

Conclusion: In arrays, manageable amounts of data should be kept for a short period of time. Because arrays are held in main memory, array operations are very fast. You can copy, sort, search for array elements in no time at all.

However, in certain circumstances you may need to use arrays with hundreds or thousands of elements. The following table shows the formula for calculating the memory usage for each array type:

Array typeFormula for memory usage in bytes
Boolean(31 + number of elements) / 8
date(1 + number of elements) * 6
String(1 + number of elements) * (sum of the size of each text)
Integer(1 + number of elements) * 2
Long integer(1 + number of elements) * 4
picture(1 + number of elements) * 4 + sum of the sizes of each image in the array
pointer(1 + number of elements) * 16
number(1 + number of elements) * 8
text(1 + number of elements) * (sum of the size of each text)
Two-dimensional(1 + number of elements) * 12 + sum of the size of each array

Hints:

  • The size of a text in the memory is calculated with the formula ((length + 1) * 2).
  • The selected element, the number of elements, and the array itself take a few extra bytes.

When working with large arrays, you should check that there is enough memory. To do this, put the array creation at the beginning and query errors with an ON ERR CALL project method. Example:

The ERROR HANDLING project method is as follows: