core/src/main/java/com/orientechnologies/common/jna/ONative.java
public MemoryLimitResult getMemoryLimit(final boolean printSteps) {
    //Perform several steps here:
    //1. Fetch physical size available on machine
    //2. Fetch soft limit
    //3. Fetch cgroup soft limit
    //4. Fetch cgroup hard limit
    //5. Return the minimal value from the list of results

    long memoryLimit = getPhysicalMemorySize();
    boolean insideContainer = false;

    if (printSteps) {
      OLogManager.instance()
          .infoNoDb(this, "%d B/%d MB/%d GB of physical memory were detected on machine", memoryLimit, convertToMB(memoryLimit),
              convertToGB(memoryLimit));
    }

    if (Platform.isLinux()) {
      final OCLibrary.Rlimit rlimit = new OCLibrary.Rlimit();
      final int result = C_LIBRARY.getrlimit(OCLibrary.RLIMIT_AS, rlimit);

      //no errors during the call
      if (result == 0) {
        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Soft memory limit for this process is set to %d B/%d MB/%d GB", rlimit.rlim_cur,
              convertToMB(rlimit.rlim_cur), convertToGB(rlimit.rlim_cur));

        memoryLimit = updateMemoryLimit(memoryLimit, rlimit.rlim_cur);

        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Hard memory limit for this process is set to %d B/%d MB/%d GB", rlimit.rlim_max,
              convertToMB(rlimit.rlim_max), convertToGB(rlimit.rlim_max));

        memoryLimit = updateMemoryLimit(memoryLimit, rlimit.rlim_max);
      }

      final String memoryCGroupPath = findMemoryGCGroupPath();

      if (memoryCGroupPath != null) {
        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Path to 'memory' cgroup is '%s'", memoryCGroupPath);

        final String memoryCGroupRoot = findMemoryGCRoot();

        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Mounting path for memory cgroup controller is '%s'", memoryCGroupRoot);

        File memoryCGroup = new File(memoryCGroupRoot, memoryCGroupPath);
        if (!memoryCGroup.exists()) {
          if (printSteps)
            OLogManager.instance().infoNoDb(this, "Can not find '%s' path for memory cgroup, it is supposed that "
                + "process is running in container, will try to read root '%s' memory cgroup data", memoryCGroup, memoryCGroupRoot);

          memoryCGroup = new File(memoryCGroupRoot);
          insideContainer = true;
        }

        final long softMemoryLimit = fetchCGroupSoftMemoryLimit(memoryCGroup, printSteps);
        memoryLimit = updateMemoryLimit(memoryLimit, softMemoryLimit);

        final long hardMemoryLimit = fetchCGroupHardMemoryLimit(memoryCGroup, printSteps);
        memoryLimit = updateMemoryLimit(memoryLimit, hardMemoryLimit);
      }
    }

    if (printSteps) {
      if (memoryLimit > 0)
        OLogManager.instance()
            .infoNoDb(this, "Detected memory limit for current process is %d B/%d MB/%d GB", memoryLimit, convertToMB(memoryLimit),
                convertToGB(memoryLimit));
      else
        OLogManager.instance().infoNoDb(this, "Memory limit for current process is not set");
    }

    if (memoryLimit <= 0)
      return null;

    return new MemoryLimitResult(memoryLimit, insideContainer);
  }
public MemoryLimitResult getMemoryLimit(final boolean printSteps) {
    //Perform several steps here:
    //1. Fetch physical size available on machine
    //2. Fetch soft limit
    //3. Fetch cgroup soft limit
    //4. Fetch cgroup hard limit
    //5. Return the minimal value from the list of results

    long memoryLimit = getPhysicalMemorySize();
    boolean insideContainer = false;

    if (printSteps) {
      OLogManager.instance()
          .infoNoDb(this, "%d B/%d MB/%d GB of physical memory were detected on machine", memoryLimit, convertToMB(memoryLimit),
              convertToGB(memoryLimit));
    }

    if (Platform.isLinux()) {
      final OCLibrary.Rlimit rlimit = new OCLibrary.Rlimit();
      final int result = C_LIBRARY.getrlimit(OCLibrary.RLIMIT_AS, rlimit);

      //no errors during the call
      if (result == 0) {
        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Soft memory limit for this process is set to %d B/%d MB/%d GB", rlimit.rlim_cur,
              convertToMB(rlimit.rlim_cur), convertToGB(rlimit.rlim_cur));

        memoryLimit = updateMemoryLimit(memoryLimit, rlimit.rlim_cur);

        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Hard memory limit for this process is set to %d B/%d MB/%d GB", rlimit.rlim_max,
              convertToMB(rlimit.rlim_max), convertToGB(rlimit.rlim_max));

        memoryLimit = updateMemoryLimit(memoryLimit, rlimit.rlim_max);
      }

      final String memoryCGroupPath = findMemoryGCGroupPath();

      if (memoryCGroupPath != null) {
        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Path to 'memory' cgroup is '%s'", memoryCGroupPath);

        final String memoryCGroupRoot = findMemoryGCRoot();

        if (printSteps)
          OLogManager.instance().infoNoDb(this, "Mounting path for memory cgroup controller is '%s'", memoryCGroupRoot);

        File memoryCGroup = new File(memoryCGroupRoot, memoryCGroupPath);
        if (!memoryCGroup.exists()) {
          if (printSteps)
            OLogManager.instance().infoNoDb(this, "Can not find '%s' path for memory cgroup, it is supposed that "
                + "process is running in container, will try to read root '%s' memory cgroup data", memoryCGroup, memoryCGroupRoot);

          memoryCGroup = new File(memoryCGroupRoot);
          insideContainer = true;
        }

        final long softMemoryLimit = fetchCGroupSoftMemoryLimit(memoryCGroup, printSteps);
        memoryLimit = updateMemoryLimit(memoryLimit, softMemoryLimit);

        final long hardMemoryLimit = fetchCGroupHardMemoryLimit(memoryCGroup, printSteps);
        memoryLimit = updateMemoryLimit(memoryLimit, hardMemoryLimit);
      }
    }

    if (printSteps) {
      if (memoryLimit > 0)
        OLogManager.instance()
            .infoNoDb(this, "Detected memory limit for current process is %d B/%d MB/%d GB", memoryLimit, convertToMB(memoryLimit),
                convertToGB(memoryLimit));
      else
        OLogManager.instance().infoNoDb(this, "Memory limit for current process is not set");
    }

    if (memoryLimit <= 0)
      return null;

    return new MemoryLimitResult(memoryLimit, insideContainer);
  }
@param printSteps Print all steps of discovering of memory limit in the log with {@code INFO} level.

@return Amount of memory which are allowed to be consumed by application, and detects whether OrientDB instance is running
inside container. If <code>null</code> is returned then it was impossible to detect amount of memory on machine.