Skip to content

How to analyze memory usageđź”—

In many cases, an increase in memory consumption occurs due to memory caching, either by HALCON or by the operating system.

This increase will usually stabilize after a finite number of runs. Unless there is an actual memory-related problem, such as an out-of-memory error, we recommend simply accepting this behavior. After all, the memory caching is there to speed up the application.
Any investigations of memory usage and any changes to HALCON’s memory management settings should ideally be carried out on the final production setup with the final application and the actual production images. This is because there are many hardware and software factors, including image content, which can affect memory behavior.

If there is an actual memory-related problem, follow these steps:

  1. Switch off HALCON’s memory caching. Enter this code at the very beginning of your application to switch off the caches:
set_system ('global_mem_cache', 'idle')
set_system ('temporary_mem_cache', 'idle')
set_system ('image_cache_capacity', 0)

If this resolves the memory problem and the increased runtime is still acceptable, keep the caches switched off.

  1. If resolving the memory issue in step 1 causes the application to become slow, consider restricting the amount of memory HALCON caches. There are various options available, such as:

    1. Switch off one or two caches instead of all.
    2. The temporary memory cache typically exerts the most significant influence on memory consumption and speed. A better balance between memory and speed can potentially be achieved by using the 'aggregate' mode and specifying an application-dependent limit for the size of the superblocks:
      set_system ('temporary_mem_cache', 'aggregate')
      set_system('alloctmp_max_blocksize', <limit_in_bytes>)
      The size of the temporary memory reservoir can be limited via the parameter 'temporary_mem_reservoir_size'. The temporary memory cache can also be put into 'shared' mode. For more information, see the section about temporary memory cache modes.
    3. Another way to limit the memory consumption of the temporary memory cache is by reducing the number of threads. For AOP threads, this can easily be done via set_system('thread_num', <number_of_AOP_threads>). For more information, see the tutorial about parallelization concepts.
    4. In some situations, for example, when using a threadpool as in .NET Tasks, the memory issue can be mitigated by switching off the temporary memory cache at the end of each task, and switching it on at the beginning of each task.
  2. Check whether mimalloc is enabled.
    mimalloc tends to cache memory more aggressively than, for example, the Win32 default heap allocator. Therefore, switching to the default allocator can help resolve memory related problems in some cases:

    set_system('memory_allocator', 'system')
  3. .NET only: Memory consumption can potentially accumulate due to the behavior of the garbage collector. For more information, see About memory management.

  4. An actual memory leak is defined as a piece of main memory that is not being released and is either no longer used or no longer accessible. Think about how to run the code suspected of leaking memory:

    1. Run the operator or piece of code in a loop.
      If there is a leak, memory consumption should rise continuously, without saturating. How many runs are necessary to decide whether it is a leak and not a caching effect depends on the amount of memory leaked. For small amounts of leaked memory, it might be necessary to run the loop for a long time.
    2. In general, using HDevelop to track memory use is problematic, as HDevelop constantly allocates and frees memory in response to user input.
      To minimize measurement noise as much as possible, export the HALCON Script as C or C++ code and run the exported code. Alternatively, reduce the memory influence from HDevelop by switching off the updates at the beginning of the script:
      dev_update_off ()
      dev_update_time ('off')
    3. Think about which tool to use for tracking memory consumption.
      As explained in Managing memory, memory already released by HALCON may still be allocated to HALCON from the operating system’s point of view. This limits the ability of any 3rd-party profiling tool to reliably determine HALCON’s actual memory consumption.

      Tip (Windows only)

      Despite this limitation, a go-to choice is the program “Performance Monitor”. It allows measuring and logging memory usage for selected processes:
      This can be a running HDevelop process (in which your script is being executed), or a compiled EXE file that utilizes HALCON. When using Performance Monitor, we recommend recording/monitoring the metric “Private Bytes” for your process. The Windows Task Manager is not recommended for thoroughly investigating the memory usage of HALCON, or any other process, as it monitors the memory usage of the entire system and has no option to focus only on the application-of-interest. Further, it does not offer options for customization or logging.