KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Heap is a lot larger than used, how to find the cause? Re: Heap is a lot larger than used, how to find the cause?

  • Laura

    Administrator
    March 14, 2023 at 12:00 am

    Hi Nick,

    The previous comment of using .Q.w[] is a good start for isolating what part of the calculations are memory intensive and requiring a large heap allocation by the OS. Printing to standard out using 0N! after each expected memory intensive line will isolate that point in your code.

    On the more under-the-hood side, this article by AquaQ is quite helpful to help understand. But to summarise and add some additional points:

    • KDB allocates memory in powers of two. Meaning a vector of data will be placed in a memory block one power of 2 up from the raw data, leading to at most 2x memory used.
    • Memory fragmentation may also be an issue depending on your aggregations – example here
    • The Q process starts with a certain amount of heap allocation that is larger than the used space (this can be seen by starting a Q session and running .Q.w[] straight away). The process won’t go below this heap allocation by the OS on startup.

    If you don’t think that a combination of these points contributes enough to cause the heap to be this much larger than used after calling .Q.gc[]  I’d recommend invoking the script from the timer manually and investigating with .Q.w from there, as the heap does appear rather large even given the above. This would eliminate the issue of running garbage collection, and the timer function running again while investigating with .Q.w causing the numbers to be misleading.