Wednesday, October 17, 2007

Delphi and C++Builder IDE Memory Usage Tip

Not only can you build .NET applications if you have the Delphi for .NET personality but the IDE uses .NET internally. If you have limited memory the .NET garbage collector might not collect the memory as often as you would like. You can force the .NET garbage collector to happen more frequently. Use Regedit and open the borland key and add a the "Globals" key if it doesn't already exist under BDS\x.0 (Example: BDS\5.0\Globals). Add a string value as follows:

CollectGarbageInterval = "300000"

This sets a timer to force garbage collection every 5 minutes (5 * 60 * 1000 = 300000).

19 comments:

Lachlan said...

How often does GC run in the IDE without this value being set?

Anonymous said...

Does this explain why my IDE memory usage spirals out of control as I build project groups with large #s of projects? (like starts at 180mb of ram used, 230mb after the IDE starts and steadily climbs past a gb of ram as the builds proceed, never to return without an IDE restart)

Yogi Yang said...

I still don't understand as to why is Delhi using .NET internally in the IDE?

If I and using the Win32 personality of Delphi then the .NET part should never get loaded at all....

Can't the very loading of .NET be bypassed altogether?

Yogi Yang

Jim McKeeth said...

It is my understanding that the IDE uses the .NET framework for the language parsing features. This is what enables all the cool new refactoring features.

One thing to keep in mind about how the .NET Framework manages memory, is once it allocates more, it doesn't give it back, even if the garbage collector runs. It just makes it available back to the application. When you are building a project group with a lot of projects in it, then it needs to allocate a lot of memory to build each additional project (this happened in Delphi 7, and maybe earlier. Whenever they introduced groups).

With .NET it just doesn't give the memory back until the OS requests it. Unused memory doesn't do you any good. If you had a terabyte of ram most of it would remain waisted. Check your commit charge in task manager. If the peak is below the limit then you are fine.

Chris B. said...

Lachlan,

If you do not set this registry key then the IDE uses the GC defaults, which is supposed to be transparent to th euser and I don't believe this is published.

Chris B. said...

Anonymous,

The only reason I'm mentioning this registry key is because I've seen others complain about the memory usage and honestly I'm currious if it will solve the problem as at times I have seen it solve memory problems for me.

When programing with a garbage collected language, it is a good idea to assign anything you are no longer using to nil/null. If you do a large task and the memory usage grows it will never be given back to the system as Jim Mckeeth noted in a comment here. After that large task it may be a good idea to call GC.Collect() to force cleanup of all unused memory and not grow the allocated memory for the process.

Chris B. said...

Yogi Yang,

Jim McKeeth is correct, the vast majority of the iDE is written in Delphi but parts of it, such as refactoring, are written in .NET and these are used even for Win32. The documentation viewer that the SDK documentation is written in is only available with .NET SDK.

Tiedo said...

Are we talking about a registry-key in HKEY_CURRENT_USER or in HKEY_LOCAL_MACHINE?
(they both have a "Borland/BDS/5.0/Globals" key)

Anonymous said...

Hi Chris,

does this work for Delphi 2006/Turbo Delphi as well?

Cheers,
Michael

Chris Bensen said...

Tiedo,

HKEY_CURRENT_USER is fine.

Chris Bensen said...

Anonymous,

This trick should work back to Delphi 8.

Germán Pablo Gentile said...

BTW ,Chris that dont helps, the IDE STLL heats one giga of memory... When you invoke code insigth it takes seconds in give response, the BSD process go to 75 % on a core duo 2 notebook. Is a shame, really, very frustrating i have to close my ide every hour of working. Seems like you guys at codegear not really use IDE in daily work, have no justification that kinds of bugs happens after 4 versions of ide. I will disable code insigth and refactoring and try if that solve. Is a real shame Codegear cannot solve that basic memory leaks, who can we expect then???

Germán Pablo Gentile said...

I forget to ask. Why dont run the compiler in a separate task, allowing to create itself, compile, and exit? Is CLEAR the compiler have mempory leaks. A simple compile with the task bar open will reveal that. So wich are the reasons to not separate that in a diferent task of the ide? In the proved event you cannot fix that memory leaks, again.

Germán Pablo Gentile said...

I can confirm removing refactoring stabilize the IDE behaviours. The memory stops growing after one day of use. So, wich i cannot undesrtand is: Is not supossed .NET have a garbage collector avioiding the memory leaks caused by bad coded software (like codegear refactoring moddule)?
Or that memory leaks have to ve to some unsafe code used by interfase some win32 part with refactoring module?

Chris Bensen said...

Germán Pablo Gentile,

Trust me, we use the IDE and we use the IDE as it is in development. We learned a lot about .NET and interop issues and just like any human made some mistakes. .NET and garbage collectors are not perfect either. If you have reproducible memory problems please enter a bug so we can look at it.

Germán Pablo Gentile said...

I trust you, but then you need to start doing some real work to really evaluate in the cook that problems. As i say, from the time i disable refactoring the ide works stable, the memory dont grow at all and never again crash!

And BTW, why you remain asking for test cases? Like i say it happens ONLY with big projects (and the most of developers works on BIG projects, i mind lot of source code lines) so how can you imagine i can provide you with my project source code? Is the same i request you for the Delphi ide source code. just bring me that and i wil find the chunk of code eating memory and crashing the ide...

NO, i cannot give you a test case, sorry. And for that reason the Codegear developer team will NEVER find the cause. And im not the only one reporting that, so wich will you do?

Chris Bensen said...

Germán Pablo Gentile,

Become a technology partner to gain access to what you are asking.

Germán Pablo Gentile said...

AFAIK, to be a CTP i must sell a product which integrate with Codegear products, not my case. I may love be a CTP, but just dont apply.

If im wrong , just tell me.

Best regards.

Chris Bensen said...

Germán Pablo Gentile,

I'm not aware of all the details. Please ask David I about the specifics.

Post a Comment