Skip to content

interop.Reference returns undefined value #134

@DeepakArora76

Description

@DeepakArora76

Hi everyone,

I need your help to understand why the vmStatRef.value.free_count in the below code returns undefine value.

I am using NativeScript 8.1. The platform is iOS.

  "devDependencies": {
    "@nativescript/android": "~8.1.1",
    "@nativescript/core": "~8.1.4",
    "@nativescript/ios": "~8.1.0",
    "@nativescript/types": "~8.1.1",
    "@nativescript/webpack": "^3.0.0",
    "prompt": "^1.2.0",
    "rimraf": "^3.0.2",
    "semver": "^7.3.5",
    "ts-node": "^10.4.0",
    "ts-patch": "^1.4.4",
    "tslint": "^6.1.3",
    "typescript": "~4.4.4"
  }

The git repository is https://github.com/DeepakArora76/nativescript-dna-deviceinfo

Here is a 64-bit implementation

  static freeMemory(): number {
    const KERN_SUCCESS = 0;
    const HOST_VM_INFO = 2;
    const hostSize = new interop.Reference(interop.sizeof(vm_statistics64)/interop.sizeof(interop.types.int64));
    const hostPort = mach_host_self();

    const pageSize = new interop.Reference(0);
    host_page_size(hostPort, pageSize);

    const vmStat = interop.alloc(interop.sizeof(vm_statistics64));

    let freeMem = -1;
    if (host_statistics64(hostPort, HOST_VM_INFO, vmStat, hostSize) === KERN_SUCCESS) {
      const vmStatRef = new interop.Reference<vm_statistics64>(vm_statistics64, vmStat);
      freeMem = vmStatRef.value.free_count * pageSize.value;
    }
    return freeMem;
  }

I also tried vm_statistics instead of vm_statistics64 and that too failed as well!

32-bit implementation

  static freeMemory(): number {
    const KERN_SUCCESS = 0;
    const HOST_VM_INFO = 2;
    const hostSize = new interop.Reference(interop.sizeof(vm_statistics)/interop.sizeof(interop.types.int32));
    const hostPort = mach_host_self();

    const pageSize = new interop.Reference(0);
    host_page_size(hostPort, pageSize);

    const vmStat = interop.alloc(interop.sizeof(vm_statistics));

    let freeMem = -1;
    if (host_statistics(hostPort, HOST_VM_INFO, vmStat, hostSize) === KERN_SUCCESS) {
      const vmStatRef = new interop.Reference<vm_statistics>(vm_statistics, vmStat);
      freeMem = vmStatRef.value.free_count * pageSize.value;
    }
    return freeMem;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions