Solved

Efficient programming

Posted on 2002-07-30
23
484 Views
Last Modified: 2010-04-04
Hi.

Our Application is an delphi ISAPI Dll .
The Client and Server has forms and the
Client has forms rich in window controls
and graphical controls.We get the "Out
of memory" error and Access violation in WINDOWS 98
when we open just 6 forms.
This is due to the limitation of Windows 98.
It's working in Windows NT/2K
Is there a workaround in delphi to avoid
such errors? I tried to design the form
with many tab pages.This reduced the use
of system resources but once we enter into
the graphical tab page the resource consumption
is more and is not regained when the tab page is
out of focus.
Is there any efficient way of programming in delphi?

Thanks and Regards
Prema
0
Comment
Question by:premaa
  • 10
  • 8
  • 3
  • +1
23 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 7187653
>> Is there any efficient way of programming in delphi?

Oh, yes, of course...   :-)

Well, in win9x you have 3 kind of resources:
(1) GDI resources (bitmaps, brushes, pens, ...).
(2) System resources (process, thread, semaphore, file, ...).
(3) User resources (windows, window stations...).

All those resources are nothing but handles. Win9x can handle 16k of those handles (if I remember correctly). If you use more, everything goes wild.

So that means, you have to be careful with all kinds of handles. Please check your sources, whether you release all handles, which you have opened. Furthermore try to avoid TPanel if possible, because each TPanel consumes one window handle. Instead use TBevel, it consumes no handles at all. Also avoid TImage and such stuff, because each of those components consumes 1 or even 2 (masked) GDI handles. Instead use image lists and bitmap/image components which work with image lists. Finally don't create all forms at once during the initialization, instead only create them if you need them and free them afterwards again.

If you follow all those tips, you should be able to significantly reduce the resouces consumation of your software.

Regards, Madshi.
0
 

Author Comment

by:premaa
ID: 7187665
Thanks for the immediate reply,
Only the system resources and GDI resources
goes up in this case..


is there any site that you know which
gives tips like these so that it'll be
helpful for us in future ..


Regards
Prema.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7187684
Hmmm... Don't know such a site in the moment.

If user resources don't go up, you don't need to care for TPanel. System resources go up as well? Do they go down if you close your forms without closing the process (when closing the process Windows automatically releases all resources)?
0
 

Author Comment

by:premaa
ID: 7187730
Yes, There are no memory leaks ,
But the system and GDI resources go up ,
For every form that is opened , 16% is used
for both the resource types ,
moving the graphical controls
to different tab pages is also not helping much.

Thanks & Regards
Prema.

0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 7187847

I create and destroy forms on the fly if the form is not visible when not in focus. Also do the same with tabbed sheets, you cannot see the contents of the other tabbed sheets so destroy them and create them when they come into view.

Regards
Steve
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7187874
16% per window is absolute overkill. Which components do you have on the form?
0
 

Author Comment

by:premaa
ID: 7187962
Sorry that i could not post immediately,
was help up in a discussion.
Thanks Steve,Currently I have no idea
about how it's done as we
are going to maintain this product
developed by some other team.
The form has many buttons,controls and images.
THe user can also customize it . He can use
many images / controls as per his need.
When i searched in the files ,  i found
TPanels,TImage and Tbitmaps are used..
using Memproof also we found immense
use of bitmaps,windows etc..

Thanks and Regards
Prema

0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 7189736
Just one correction I would like to submit not that it helps your current situation but I thought I would pass on this info anyway.

"System resources are areas of memory that are used by the input manager (USER) and the graphic display interface manager (GDI) for keeping track of all of the windows that are open in a session and for drawing objects on the screen. Stored in memory in  these heaps and the size of each heap is fixed."

16-bit User heap (64K)
32-bit User window heap (2MB)
32-bit User menu heap (2MB)
16-bit GDI heap (64K)
32-bit GDI heap (2MB)
-------------------------

Also don't confuse system with being a separate resource. There are only two the USER and the GDI. The SYSTEM one just displays which one of the two that has the lowest percentage. So in this case your problem seems to stem from the GDI, which is the graphics interface. Try reducing the number of colors, pens, bushes, and fonts being used on/in the form and or in the application. Reuse redundant objects instead of creating separate individual ones.
-------------------------
 
"The USER heap contains information about windows being used by active applications. The data structure for each window, including any minimized windows, is stored in this heap. Examples of windows include application windows, dialog boxes, and controls (such as buttons and check boxes).

The GDI heap contains graphical objects, such as pens, brushes, cursors, fonts, and icons. "
-------------------------

These links give some detail info about the System Resources although it has nothing to do with programming per se the information nonetheless could be useful.

http://www.windows-help.net/techfiles/win-resources.html
http://www.techweb.com/winmag/columns/explorer/2000/11.htm


The Crazy One
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7189874
Hi CrazyOne,

you seem to be right in saying that "system" just shows the lower of the other two. I always thought it would be a seperate resource, because there do exist 3 kinds of handles (GDI, user and kernel handles). So I always thought the "system" resource value would show the usage of kernel handles. But after thinking about it, this might be wrong. Kernel handles are quite different from the other 2 kinds, so it's very well possible, that there's no "kernel resource".

Regards, Madshi.
0
 

Author Comment

by:premaa
ID: 7190099
Thanks CrazyOne for that information regarding system Resource ,
Our application seems to use
lot of bitmap handles.
We found out using Memproof that bitmap and
window handles go up on opening a form depending
on the number and type of controls on the form.
Is there any way the form can be redesigned ?
Can u please let us know the alternate for TBitmap like u suggested
for TPanel..? Can you please give any more of such
tips ?

Thanks and Regards
Prema.








0
 
LVL 20

Accepted Solution

by:
Madshi earned 200 total points
ID: 7190124
Look, a TBitmap is an encapsulation of a Windows GDI bitmap. Same with TImage. This consumes 1 handle or 2 handles (if the bitmap has a mask) for each TImage. To save resources, 32bit Windows has introduced image lists. An image list can contain thousands of images and only consumes 1-2 handles, regardless of how many bitmaps it contains. However, all images in one image list have to have the same size and color depth.

There are free controls out there, which work more or less similar to TImage, but don't have a Picture property. Instead they directly work with a TImageList, so they consume no handles at all (only the image list).

Look on the well known Delphi freeware components pages (e.g. www.torry.net ). I'm quite sure there are several such components available.

Regards, Madshi.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:premaa
ID: 7190225
Hi ,
May be using this TBitmap is the main problem.
Bcos more user resources are free than GDI
For one form , Memproof showed
LivePointer 10921
Global heap 41
Bitmaps 209
Brush 20
Palette 51
Window 177
These numbers go up once the form is loaded..

Thanks & Regards
Prema.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7190234
Windows and Bitmaps are both too high, if you ask me...
0
 

Author Comment

by:premaa
ID: 7190241
Hi ,
Thanks for the immediate reply.
But the form has a number of controls.
May be this is bcos of the TPanel.
So , Instead of using TBitmap if we
use TImagelist will the number of handles
get reduced? Can we recommend this solution?


Thanks
Prema.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7190290
I was recommending this solution already with my first comment...   :-)
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 7190459
Hi Madshi

Yeah I think for some reason there isn't the same tight restrictions put on the kernel handles as there are for the other two. I am not sure why this is though.

premaa

I think Madshi may be correct in advising to use TImagelist to help reduce the handles. Try it and see what happens.

Sometimes depending on the application and what it is intended to do you sometimes can trim how much of the System Resources get use. However there are times that the application will cause a big hit on the System Resources and there is not much that can be done about it. It is not entirely unusual although somewhat rare to see a program use over 15% of the System Resources. Example take a look at how much Delphi uses on a complex application while your doing Step Over debugging. The main reason I switched to Win2000 was because when doing a lot of debugging in Delphi with Win98 the System Resources would steadily drop to the point that I had to close Delphi. Sometimes I had to reboot because sometimes not all of the Resources that Delphi had used would be released.
0
 

Author Comment

by:premaa
ID: 7192514
Hi,
 When we browsed Delphi help, we found a
method TBitmap.Dormant which frees GDI
resources ..Will it be useful? this is not used
in our code at all.
They have also used Timagelist.
Please let me know whether adding this
function will help

Thanks and regards
Prema
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 7192584
Hmm never knew about that method before. I don't use a lot of BitMaps so I couldn't tell how effective the Dormant method would be but reading the help file it seem like the main function of it is to release the handle from the GDI. Sounds good to me. It may use more memory but chances that shouldn't be a big issue of course that all depends on the system it runs on and how many DIB's are being used and how many other programs are running at the same time.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7192594
Just try it out. I'm not sure whether it helps. I think replacing the bitmaps would be a better choice.
0
 

Author Comment

by:premaa
ID: 7192620
Thanks , I'll try out and let u know by today..

Regards
Prema.
0
 

Author Comment

by:premaa
ID: 7208892
Hi ,
 There was some animation in my form , I was able to
open 2 more forms when i commented that part of code..
is there any property in Delphi tab page control
to create controls on the page dynamically only
when the user clicks, similar to "createondemand" property
in Powerbuilder.?

Thanks and Regards
Prema.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 7209558
That's the idea!!
TPageControl.OnChange fires when you select a new tab. Have the controls created for the page then.
Regards,
Steve
0
 

Author Comment

by:premaa
ID: 7219766
Hi ,
 Is there any control that can host other controls
and take up less number of handles like the datawindow
in Powerbuilder.?

Thanks and Regards
Prema.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now