Saturday, March 15, 2008

Reduce memory usage of LCL

To test the conclusions of the last post i modified the field order of some LCL classes to group together Boolean fields.

Here's the return value of the InstanceSize property:

TButton
before: 874 bytes
after: 829 bytes (saved 24, 16 and 5 bytes in TControl, TWinControl and TCustomButton respectively)

TMenuItem
before: 152 bytes
after: 136 bytes

In an application with 100 controls and 20 menu items you save 2400 (considering only TControl memory) and 320 bytes respectively.

Maybe be this is negligible in computers with 1GB or more of RAM, but for mobile platforms it makes a difference.

The patch is here. Have fun!

5 comments:

Paul Ishenin said...

Very interesting. Why not to post to lazarus/fpc mail list? I don't think this blog is well known (though I am reading it from time to time).

Luiz Américo said...

I'll post a link.

BTW i was planning to propose the same for the Qt interface. I already take a look and found places where can be changed, just lazy to do the patch.

Paul Ishenin said...

I commited your patch in r15201 though effect is not very noticable:

laz ide:
memory virtual memory
before 21544 16208
after 21512 16188
---------------------------------
diff 32KB 20KB

So effect is about 0.1% for the ide :)

Luiz Américo said...

The expected reduction is not really very significant. In fact is surprising: 20kb means 1000 TControl/TMenuItem, and i doubt the IDE has all this. Maybe there's some side effect.

Be aware that fpc, AFAIK, allocates large blocks of memory to reduce memory fragmentation, so the virtual memory may not represent the real usage.

Luiz Américo said...

BTW, i have other ideas to reduce memory usage, but those, unlike this, can be classified between intrusive and hack.

I'll post some articles ASAP.