debug

October 22, 2022

debug

[New in Pro 8.7.0]

The Debug module provides some debugging tools, such as diagnosing memory leaks, getting a detailed stack of an Error, etc.

$debug.dumpHprof(file)

  • file {string} dump file path

Dump the memory of the entire script process to the file file.

When you find that CloudControl Pro is taking up a lot of memory, you can run this function to dump the entire memory and give feedback to the developer, who can use the memory dump file to diagnose if there is a memory leak.

During the dump process, the whole process will be stuck, please don't operate your phone at this time, in order to cause dump failure or other problems, etc.; the dump usually takes a few tens of seconds to a few minutes, please wait patiently.

How to send the file to the developer? You can attach your script and dump file and send it to 2874662633@qq.com, the developer will check and reply as soon as possible. It is also recommended to enable memory leak checking by $debug.setMemoryLeakDetectionEnabled() function before feedback to troubleshoot memory leaks in scripts to prevent oops and reduce developer's workload.

$debug.dumpHprof('. /dump.hprof');

$debug.dumpAndSendHprof([file])

  • file {string} The path to the dump file, optional. Defaults to dump.hprof.zip in the current directory.

Dump the memory of the entire script process into the file file and automatically compress it to a zip file. Uses the highest compression level, so it takes longer, but the file is smaller.

See $debug.dumpHprof for more information.

$debug.getStackTrace(error)

  • error {Error} exception/error
  • return {string}

Gets the detailed stack of an exception and returns it.

try {
    undefined_var;
} catch(e) {
    console.error($debug.getStackTrace(e));
}

$debug.setMemoryLeakDetectionEnabled(enabled)

  • enabled {boolean} Whether to enable memory leak detection

When memory leak detection is enabled, objects that are not manually reclaimed, such as image objects, will be printed in the log.

The objects currently detected include.

  • image images

For example, the following code will cause a memory leak and you should see the leak log in the log some time after running.


$debug.setMemoryLeakDetectionEnabled(true);
requestScreenCapture();

for (let i = 0; i < 10; i++) {
    // This image should have been reclaimed manually by calling recycle
    let leak = captureScreen().clone();

    // We intentionally commented out the recycle code
    // leak.recycle();
}
// trigger gc
$debug.gc();

This is turned on by default when CloudControl Pro is running; it is turned off by default in packaged software.

$debug.gc()

Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.

The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly. The method System.gc() is the conventional and convenient means of invoking this method.

Last update:
Contributors: Bruce