Recent

Author Topic: CMem - questions about C memory manager  (Read 4488 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1977
    • http://wiki.lazarus.freepascal.org/User:Zoran
CMem - questions about C memory manager
« on: November 15, 2016, 12:22:34 pm »
I'm writing a multithreaded application, and I see that this example in Multithreaded Application Tutorial adds CMem (but only inside ifdef unix):
Code: Pascal  [Select][+][-]
  1. program MyMultiThreadedProgram;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$ifdef unix}
  5.   cthreads,
  6.   cmem, // the c memory manager is on some systems much faster for multi-threading
  7. {$endif}
  8.   Interfaces, // this includes the LCL widgetset
  9.   Forms
  10.   { you can add units here },
  11.  

What do you say, when should we include CMem? Should we use it when we have many threads? Should we use it in applications without multithreading?
Should we use it on Unix based targets only (as in this example, where it is put inside "ifdef unix" directive)?

And another question - the docs (see links below) say that CMem should be the first unit in uses section of the program, but in the above example it is put after CThreads, and CThreads documentation also say similar thing - it should be "preferably the very first unit" in uses section of the program.
So, which of these two units should be the first?

The fpc documentation about cmem unit:
http://www.freepascal.org/docs-html-3.0.0/rtl/cmem/index.html
and wiki page:
http://wiki.lazarus.freepascal.org/CMem

Thanks,
Zoran
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: CMem - questions about C memory manager
« Reply #1 on: November 15, 2016, 12:56:11 pm »
You should include cmem only in two cases:
1. If a library written in another language allocates memory that you are supposed to free on your side. FPC's memory manager can choke on that.
2. if you have a predictable memory use over large structures (not strings) In that case the cmem memory manager is much faster because it is simpler.

But for normal cases stick to whatever FPC offers you. It's memory manager is not THAT bad (unlike what Marco thinks I always say :) ) and optimizes memory use in favor of speed.

Note that you should include cmem before anything other, so your code is wrong anyway. First cmem, then cthreads.
Otherwise you'll end up with two memory managers managing your code,which is a bad idea.

If you use the heap debugging feature (-gh) never use cmem: you can't debug properly. It relies on it's own memory manager.
And if you do specify -gh NEVER EVER use heapmm in your uses clause. The compiler puts it in the right place.
« Last Edit: November 15, 2016, 01:10:34 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: CMem - questions about C memory manager
« Reply #2 on: November 15, 2016, 01:36:24 pm »
Note that you should include cmem before anything other, so your code is wrong anyway. First cmem, then cthreads.
Otherwise you'll end up with two memory managers managing your code,which is a bad idea.
Looking at cthreads code, I think it's safe just for the two to be swap-able. cthreads doesn't call any of the RTL memory management routines, it relies on pthread functions instead.

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: CMem - questions about C memory manager
« Reply #3 on: November 15, 2016, 01:42:22 pm »
Note that you should include cmem before anything other, so your code is wrong anyway. First cmem, then cthreads.
Otherwise you'll end up with two memory managers managing your code,which is a bad idea.
Looking at cthreads code, I think it's safe just for the two to be swap-able. cthreads doesn't call any of the RTL memory management routines, it relies on pthread functions instead.

That's not the case. The case is that you can not rely on current code. You should rely on design principles. <grumpy mode on> You're fired. >:D

The design principles say the memory manager should be the first unit included in your project.
In the current trunk documentation this issue is even addressed specifically.

Never assume that what you see is what you get. That's sheer stupidity.. Sorry.
« Last Edit: November 15, 2016, 01:46:34 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zoran

  • Hero Member
  • *****
  • Posts: 1977
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: CMem - questions about C memory manager
« Reply #4 on: November 15, 2016, 02:24:17 pm »
You should include cmem only in two cases:
1. If a library written in another language allocates memory that you are supposed to free on your side. FPC's memory manager can choke on that.
2. if you have a predictable memory use over large structures (not strings) In that case the cmem memory manager is much faster because it is simpler.

But for normal cases stick to whatever FPC offers you. It's memory manager is not THAT bad (unlike what Marco thinks I always say :) ) and optimizes memory use in favor of speed.


Thank you!
Then I think that I do not need CMem for now.

As for the order, then CMem should be the first and CThreads can be moved to second position.
The docs for CMem is really more strict about it:
Quote
the cmem unit should be the first unit of the uses clause of the program
compared to CThreads:
Quote
preferably the very first unit,
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

 

TinyPortal © 2005-2018