Recent

Author Topic: TPageControl and memory  (Read 2187 times)

valter.home

  • Jr. Member
  • **
  • Posts: 81
TPageControl and memory
« on: September 04, 2020, 01:38:50 pm »
Hi everyone.
In my application I would like to use a TPageControl instead of using several Forms.
However, each page should contain numerous controls and in some cases even images that could be very large.
I've always wondered what impact it can have on memory.
When I load the main form that contains the TPageControl I imagine that all the contents of all the pages are loaded.

Could there be a way to load the content of each page only when the user selects the corresponding tabsheet?

Could there be workarounds bearing in mind that the application should remember the changes made on a page when returning to it (eg a page will contain a TscrollBox, a TImage and a TPaintBox where the user can draw shapes)?

Do you have any suggestions?

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: TPageControl and memory
« Reply #1 on: September 04, 2020, 01:55:31 pm »
Yes.

If you are working with image editing Then I would suggest using a single work image control or some sort of class and direct the Canvas to it..

 Cache the images to file and when  you switch tabs reload the image and free the other for example.

 If all tabs use the same controls then I would suggest using a simple PageControl with tools on it where you may have a set of different tools to work with.. from there use a viewing control for the image and maybe another TabControl to select the image..

 At least with this logic you only have one image loaded in memory at a time.
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: TPageControl and memory
« Reply #2 on: September 04, 2020, 01:56:26 pm »
I think basically using TPageControl and several forms consume roughly the same memory usage. Because if you add a form in Lazarus, it will be (set to) auto-created when program start and closing a form will not really freeing the form. Unless you manually change the 'behavior'.

Could there be a way to load the content of each page only when the user selects the corresponding tabsheet?

Yes, you can. By using OnChange and/or OnChanging. I haven't tried but it should be something like this: load the image in OnChange event and freeing it on the OnChanging event.

Could there be workarounds bearing in mind that the application should remember the changes made on a page when returning to it (eg a page will contain a TscrollBox, a TImage and a TPaintBox where the user can draw shapes)?

Very possible but not easy. You can save the settings to memory or to a file, maybe using TIniFile: https://wiki.freepascal.org/Using_INI_Files

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: TPageControl and memory
« Reply #3 on: September 04, 2020, 02:13:34 pm »
In my application I would like to use a TPageControl instead of using several Forms.
However, each page should contain numerous controls and in some cases even images that could be very large.
How large is that?

Yes, you can. By using OnChange and/or OnChanging.
You could use frames for each tab: instantiate them as Handoko suggested in the OnChange event.
Easier to develop and test.
If you simply don't free them, then the state of the tab is already automatically preserved by the components on the frame inside the tab.
Data is only loaded progressively every time the user opens a new tab.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

valter.home

  • Jr. Member
  • **
  • Posts: 81
Re: TPageControl and memory
« Reply #4 on: September 04, 2020, 02:36:04 pm »
Thanks for all your suggestions.

Indeed, more than the controls (different for each page) are the images that could affect the memory. Since I draw on TPaintBox I can eliminate the background image (normally a PNG that can even exceed 5k) and reload it when I return to the tab. The drawn shapes will be preserved.
I'll do some tests.

Quote
I think basically using TPageControl and several forms consume roughly the same memory usage

You're right, I hadn't really thought about it.


jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: TPageControl and memory
« Reply #5 on: September 04, 2020, 04:11:31 pm »
I've done what you are doing and ended up making a a class to handle it all..

TImageCache..

 It pointes to a folder of your choice, mainly the user folder and it's index and also has object settings in it to point to the host surface.

 When I free it I also free the cache folder ..
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TPageControl and memory
« Reply #6 on: September 04, 2020, 11:38:20 pm »
I think basically using TPageControl and several forms consume roughly the same memory usage. Because if you add a form in Lazarus, it will be (set to) auto-created when program start and closing a form will not really freeing the form. Unless you manually change the 'behavior'.

However, creating forms dynamically, and freeing them when no longer needed to minimise memory load for large objects is usually much easier than creating tabsheets dynamically and inserting them into and later removing them from a pagecontrol (though it can be done). This also has the disadvantage that it is very non-standard behaviour, even if it reduces overall memory use.
Users don't expect page control tabs to disappear with no obvious way to get them back again.
Whereas users are usually very familiar with modal dialogs (forms) that are dismissed when they have served their purpose.
Could there be a way to load the content of each page only when the user selects the corresponding tabsheet?
Quote from: Handoko
Yes, you can. By using OnChange and/or OnChanging. I haven't tried but it should be something like this: load the image in OnChange event and freeing it on the OnChanging event.
You would have to use OnChanging, since that anticipates the showing of the new page. If you wait until OnChange to add GUI content the next tabsheet is already shown, and you would probably get odd effects, flicker or worse, depending on the widgetset.
« Last Edit: September 04, 2020, 11:40:14 pm by howardpc »

af0815

  • Hero Member
  • *****
  • Posts: 1392
Re: TPageControl and memory
« Reply #7 on: September 05, 2020, 07:25:26 am »
I am using Frames and create these if a tabsheet is opened and destroy if the tabsheet is leaved. So i have organized all in different frames. In the mainform i have a manager to give every frame the needed data and so the frames can make a frame to frame communication.
regards
Andreas

valter.home

  • Jr. Member
  • **
  • Posts: 81
Re: TPageControl and memory
« Reply #8 on: September 05, 2020, 11:30:37 am »
Perhaps in this specific case the best solution could be the use of frames.

Quote
If you simply don't free them, then the state of the tab is already automatically preserved by the components on the frame inside the tab.

But if I don't destroy them at every tab change (using the main application to keep track of the changes) I don't think there would be great differences in the use of memory. Or would it not change anything as the frames are created when the application is started?

« Last Edit: September 05, 2020, 11:39:56 am by valter.home »

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: TPageControl and memory
« Reply #9 on: September 05, 2020, 11:35:12 pm »
I don't know how much memory you expect to use however, there is a TZipper component in the libs that will allow you to compress to stream so that means you can keep compressed images in memory and uncompressed them when viewing them again.
The only true wisdom is knowing you know nothing

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: TPageControl and memory
« Reply #10 on: September 07, 2020, 02:30:05 pm »
But if I don't destroy them at every tab change (using the main application to keep track of the changes) I don't think there would be great differences in the use of memory. Or would it not change anything as the frames are created when the application is started?
True.
That's why I asked how large 'large' is.
If you are talking about some megabytes then that might not be an issue at all; depending on your platform.
If you have many tabs with many MB's of data, it piles up.
Then indeed you can free the frames once you leave the tab.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

 

TinyPortal © 2005-2018