0

My question is divided into two parts that are linked together.

TargetOS: Windows, Linux.

  1. Will all other threads be blocked from allocating new memory until the OS returns the allocated memory when we allocate (new, malloc, zalloc, talloc, jalloc,...) memory in C++ (or C) in a thread?

  2. If the answer is yes, in the memory hardware's features they write cl16, cl18, and so on (I mean latency), is it time (our blocking time here) to allocate memory?

Actually, my main question is for the second part, because I want to know how the latency factor can make sense when allocating?

Another HM
  • 148
  • 10
  • @n.m. your right. edited, and if some specialized OS has different behaviors , please inform me. – Another HM Feb 14 '23 at 10:47
  • 2
    What is a _"memory hardware cartoon"_? What is _"cl16"_ and _"cl18"_? – Jabberwocky Feb 14 '23 at 10:48
  • @Jabberwocky , I mean CAS latency , that is written on the memory hardware box. – Another HM Feb 14 '23 at 10:49
  • memory allocation is expensive but is not generally single threaded. Note that memory allocation might not actually do much, in Linux pages aren't actually physically allocated until they're written to – Alan Birtles Feb 14 '23 at 10:54
  • what memory hardware box? You seem to refer to some specific illustration or article, but miss to provide a link – 463035818_is_not_an_ai Feb 14 '23 at 10:56
  • @463035818_is_not_a_number forgot that word :| I just mean the latency , maybe in our f***ing country they write :| – Another HM Feb 14 '23 at 11:00
  • 4
    CAS latency has nothing to do with memory allocation. Memory hardware knows nothing about allocations. CAS latency is the delay required to fetch data from a DDR module. – n. m. could be an AI Feb 14 '23 at 11:01
  • @n.m.: Since the memory allocator will likely not perform a sequential read (unless the memory allocations are smaller than a cache line or have a fixed stride), my guess is that on most `malloc` implementations, the CAS latency will be the most significant factor, assuming that the memory is not already in the CPU cache. – Andreas Wenzel Feb 14 '23 at 11:09
  • 2
    If you want memory allocation to be parallelized so that multiple threads do not block each other, you can use mutliple heaps. See this question for further information: [Is there any benefit to use multiple heaps for memory management purposes?](https://stackoverflow.com/q/19984851/12149471) – Andreas Wenzel Feb 14 '23 at 11:10
  • 1
    If you want to ask the second question, you should not ask the first question, but should rather state what assumptions you made about the first question, so that the second question is answerable. For example, you should state whether the threads are blocking each other or whether the threads are using different heaps, so that they do not block each other. – Andreas Wenzel Feb 14 '23 at 11:27
  • @AndreasWenzel "the CAS latency will be the most significant factor" Perhaps, but from here to "cl18 is the time to allocate memory" is quite a distance. A single allocation may need hundreds of memory reads, and how many exactly depends on many factors. – n. m. could be an AI Feb 14 '23 at 11:35
  • @n.m. oops , so it depends on that reading other blocks to find free memory too? – Another HM Feb 14 '23 at 11:42
  • @AnotherHM: I'd suggest that you do some experimentation. You can learn a lot from that. Exercise: Write a class that allocates one big chunk of 100 MB, and then implement `malloc` and `free` methods on that class. Don't make it multi-threaded at first, just see what the implementation challenges are. – MSalters Feb 14 '23 at 12:57
  • @MSalters I did same on quickbench site (some thing like google benchmark) , but I want to know that cl16 (cl18,...) how make sense in allocating. thanks for hint. – Another HM Feb 14 '23 at 14:03
  • @AnotherHM: You typically have 3 levels of CPU cache before you hit main memory, and some of that cache is shared between treads(cores). You need a much better understanding of how allocators work; at this point we cannot give you a meaningful answer. – MSalters Feb 14 '23 at 14:31

0 Answers0