function GetTotalMemorySize: UInt64; {$IFDEF IOS or MACOS} begin // Result := TUIDevice2.Wrap(TUIDevice2.OCClass.currentDevice).totalMemory; Result := NSRealMemoryAvailable; end; {$ENDIF} {$IFDEF MSWINDOWS} var lpBuffer : TMEMORYSTATUSEX; begin Result := 0; ZeroMemory(@lpBuffer, Sizeof(TMEMORYSTATUSEX)); lpBuffer.dwLength := Sizeof(TMEMORYSTATUSEX); GlobalMemoryStatusEx(lpBuffer); Result := lpBuffer.ullTotalPhys; end; {$ENDIF} {$IFDEF ANDROID} var Mgr: JActivityManager; MgrNative: JObject; MemInfo: JActivityManager_MemoryInfo; AStrings: TStringList; TempValue: string; AReader: JReader; ABufferedReader: JBufferedReader; begin Result := 0; MgrNative := SharedActivityContext.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE); if Assigned(MgrNative) then begin Mgr := TJActivityManager.Wrap((MgrNative as ILocalObject).GetObjectID); MemInfo := TJActivityManager_MemoryInfo.JavaClass.init; Mgr.getMemoryInfo(MemInfo); try Result := UInt64(MemInfo.totalMem); except //API level < 16 try Result := UInt64(MemInfo.availMem); except Result := 0; end; if FileExists('/proc/meminfo') then begin AStrings := TStringList.Create; try AStrings.LineBreak := sLineBreak; AStrings.NameValueSeparator := ':'; AStrings.Clear; AReader := TJFileReader.JavaClass.init(StringToJString('/proc/meminfo')) as JReader; ABufferedReader := TJBufferedReader.JavaClass.init(AReader); repeat TempValue := JStringToString(ABufferedReader.readLine); if TempValue <> '' then begin AStrings.Add(TempValue); end; until (not ABufferedReader.ready); ABufferedReader.close; TempValue := AStrings.Values['MemTotal'].Trim; AStrings.Clear; AStrings.NameValueSeparator := ' '; AStrings.Add(TempValue.Trim); TempValue := AStrings.Names[0]; // ShowMessage(TempValue); Result := StrToInt64Def(TempValue, Result div 1024) * 1024; finally FreeAndNil(AStrings); end; end; end; end; end; {$ENDIF}
function GetFreeMemorySize: UInt64; {$IFDEF IOS or MACOS} begin //Result := Max(0, GetTotalMemorySize - // TUIDevice2.Wrap(TUIDevice2.OCClass.currentDevice).userMemory); Result := NSRealMemoryAvailable; end; {$ENDIF} {$IFDEF MSWINDOWS} var lpBuffer : TMEMORYSTATUSEX; begin Result := 0; ZeroMemory(@lpBuffer, Sizeof(TMEMORYSTATUSEX)); lpBuffer.dwLength := Sizeof(TMEMORYSTATUSEX); GlobalMemoryStatusEx(lpBuffer); Result := lpBuffer.ullAvailPhys; end; {$ENDIF} {$IFDEF ANDROID} var Mgr: JActivityManager; MgrNative: JObject; MemInfo: JActivityManager_MemoryInfo; begin Result := 0; MgrNative := SharedActivityContext.getSystemService(TJContext.JavaClass.ACTIVITY_SERVICE); if Assigned(MgrNative) then begin Mgr := TJActivityManager.Wrap((MgrNative as ILocalObject).GetObjectID); MemInfo := TJActivityManager_MemoryInfo.JavaClass.init; Mgr.getMemoryInfo(MemInfo); try Result := UInt64(MemInfo.availMem); except Result := 0; end; end; end; {$ENDIF}
function GetTotalSpaceSize(Path: string = PathDelim): UInt64; {$IFDEF IOS or MACOS} var Dict: NSDictionary; P: Pointer; const FoundationFwk: string = '/System/Library/Frameworks/Foundation.framework/Foundation'; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; Dict := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager).attributesOfFileSystemForPath(NSStr(Path), nil); if Dict = nil then Exit; P := Dict.objectForKey((CocoaNSStringConst(FoundationFwk, 'NSFileSystemSize') as ILocalObject).GetObjectID); if Assigned(P) then Result := TNSNumber.Wrap(P).unsignedLongLongValue; end; {$ENDIF} {$IFDEF MSWINDOWS} var lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes: Int64; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; lpTotalNumberOfBytes := MaxInt; if GetDiskFreeSpaceEx(pchar(ExtractFileDrive(Path)), lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes) then begin Result := UInt64(lpTotalNumberOfBytes); end; end; {$ENDIF} {$IFDEF ANDROID} var AJFile: JFile; AJStatFs: JStatFs; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; AJFile := TJFile.JavaClass.init(StringToJString(Path)); if not Assigned(AJFile) then exit; AJStatFs := TJStatFs.JavaClass.init(AJFile.getPath); if not Assigned(AJStatFs) then exit; Result := UInt64(AJStatFs.getBlockSize) * UInt64(AJStatFs.getBlockCount); end; {$ENDIF}
function GetFreeSpaceSize(Path: string = PathDelim): UInt64; {$IFDEF IOS or MACOS} var Dict: NSDictionary; P: Pointer; const FoundationFwk: string = '/System/Library/Frameworks/Foundation.framework/Foundation'; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; Dict := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager).attributesOfFileSystemForPath(NSStr(Path), nil); if Dict = nil then Exit; P := Dict.objectForKey((CocoaNSStringConst(FoundationFwk, 'NSFileSystemFreeSize') as ILocalObject).GetObjectID); if Assigned(P) then Result := TNSNumber.Wrap(P).unsignedLongLongValue; end; {$ENDIF} {$IFDEF MSWINDOWS} var lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes: Int64; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; lpTotalNumberOfFreeBytes := MaxInt; if GetDiskFreeSpaceEx(pchar(ExtractFileDrive(Path)), lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes) then begin Result := UInt64(lpTotalNumberOfFreeBytes); end; end; {$ENDIF} {$IFDEF ANDROID} var AJFile: JFile; AJStatFs: JStatFs; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; AJFile := TJFile.JavaClass.init(StringToJString(Path)); if not Assigned(AJFile) then exit; AJStatFs := TJStatFs.JavaClass.init(AJFile.getPath); if not Assigned(AJStatFs) then exit; Result := UInt64(AJStatFs.getBlockSize) * UInt64(AJStatFs.getFreeBlocks); end; {$ENDIF}
function GetAvailableSpaceSize(Path: string = PathDelim): UInt64; {$IFDEF IOS or MACOS} begin Result := GetFreeSpaceSize(Path); end; {$ENDIF} {$IFDEF MSWINDOWS} var lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes: Int64; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; lpFreeBytesAvailableToCaller := MaxInt; if GetDiskFreeSpaceEx(pchar(ExtractFileDrive(Path)), lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes) then begin Result := UInt64(lpFreeBytesAvailableToCaller); end; end; {$ENDIF} {$IFDEF ANDROID} var AJFile: JFile; AJStatFs: JStatFs; begin Result := 0; if DirectoryExists(Path) or FileExists(Path) then begin end else begin raise Exception.Create('Path " ' + Path + '" not found.'); end; AJFile := TJFile.JavaClass.init(StringToJString(Path)); if not Assigned(AJFile) then exit; AJStatFs := TJStatFs.JavaClass.init(AJFile.getPath); if not Assigned(AJStatFs) then exit; Result := UInt64(AJStatFs.getBlockSize) * UInt64(AJStatFs.getAvailableBlocks); end; {$ENDIF}
----------------------------------------------
(C)(P)Flying Wang