speicher frei machen



  • Hallo,

    habe versucht über die winapi Anhand eines VB Beispiels eine Möglichkeit zu schaffen um Speicher freizugeben.

    Das Problem ist nur irgendwo in der Übersetzung nach C muss ein Fehler sein,
    es passiert das Gegenteil.

    Vieleicht kann es einer Lösen!!!!!!!!! 🙂

    Gruß IcemanX
    🕶

    Übersetzung nach C

    int releaseByte = StrToInt(Label_AvailPhysMem->Caption);
      LPVOID BaseAddr = VirtualAlloc(0, releaseByte, MEM_RESERVE, PAGE_NOACCESS);
    
      if(BaseAddr == 0){
       Application->MessageBoxA("can´t reserve memory for release,\nrelease memory failed!", "", MB_OK + MB_ICONWARNING);
       return;
      }
    
      BaseAddr = VirtualAlloc(BaseAddr, releaseByte, MEM_COMMIT, PAGE_READWRITE);
    
      if(BaseAddr == 0){
        VirtualFree(BaseAddr, 0, MEM_RELEASE);
        Application->MessageBoxA("can´t commit memory for release,\nrelease memory failed!", "", MB_OK + MB_ICONWARNING);
        return;
      }
    
      SYSTEM_INFO SysInfo;
      GetSystemInfo(&SysInfo);
      int Granularity = SysInfo.dwAllocationGranularity;
    
      LPVOID ReadAddress;
      ReadAddress = (char *) malloc(Granularity*1024);
      LPVOID WriteAddress = BaseAddr;
    
      while((int)WriteAddress < (int)BaseAddr + releaseByte){
        CopyMemory(WriteAddress, ReadAddress, Granularity);
        WriteAddress = (LPVOID)((int)WriteAddress + Granularity);
      }
    
      VirtualFree(BaseAddr, releaseByte, MEM_DECOMMIT);
      VirtualFree(BaseAddr, 0, MEM_RELEASE);
    

    Orginal VB

    Private Sub cbFree_Click()
    
    Dim HowMuch As Long
    Dim PagesToFree As Long
    Dim BytesToFree As Long
    Dim strTemp As String
    
    Dim BaseAddr As Long
    Dim WriteAddress As Long
    Dim ReadAddress As Long
    
    Dim ProgressVal As Long
    
    On Error GoTo Handler
    
    HowMuch = CLng(txtMB.Text) * nkb * nkb
    
    'Round to a number of pages so an exact number of pages is allocated
    PagesToFree = HowMuch / Granularity
    BytesToFree = PagesToFree * Granularity
    
    If BytesToFree <= 0 Then
        lblCantFree.Caption = "Not a valid positive number"
        lblCantFree.Visible = True
        Exit Sub
    End If
    
    If BytesToFree <= MS.dwAvailPhys Then
        lblCantFree.Caption = "That amount is already free"
        lblCantFree.Visible = True
        Exit Sub
    End If
    
    lblPages.Caption = Format$(PagesToFree, fmt)
    lblBytesToFree.Caption = Format$(BytesToFree, fmt)
    lblCantFree.Visible = False
    
    ProgressBar1.Min = 0
    ProgressBar1.Max = PagesToFree
    ProgressBar1.Value = 0
    ProgressBar1.Visible = True
    
    strTemp = String$(Granularity, Asc("a"))
    BaseAddr = VirtualAlloc(ByVal 0&, BytesToFree, MEM_RESERVE, PAGE_NOCACHE Or PAGE_READWRITE)
    
    If BaseAddr = 0 Then
        lblCantFree.Caption = "MEM_RESERVE failed"
        lblCantFree.Visible = True
        Exit Sub
    End If
    
    lblBaseAddr.Caption = Format$(BaseAddr, fmt)
    Me.Refresh
    Me.MousePointer = vbHourglass
    
    BaseAddr = VirtualAlloc(ByVal BaseAddr, BytesToFree, MEM_COMMIT, PAGE_NOCACHE Or PAGE_READWRITE)
    If BaseAddr = 0 Then
        Call VirtualFree(ByVal BaseAddr, 0&, MEM_RELEASE)
        lblCantFree.Caption = "MEM_COMMIT failed"
        lblCantFree.Visible = True
        Exit Sub
    End If
    
    WriteAddress = BaseAddr
    ReadAddress = StrPtr(strTemp)
    While WriteAddress < BaseAddr + BytesToFree
        Call CopyMemory(ByVal WriteAddress, ByVal ReadAddress, Granularity)
        WriteAddress = WriteAddress + Granularity
        ProgressVal = ProgressVal + 1
        ProgressBar1.Value = ProgressVal
    Wend
    Call VirtualFree(ByVal BaseAddr, BytesToFree, MEM_DECOMMIT)
    Call VirtualFree(ByVal BaseAddr, 0&, MEM_RELEASE)
    ProgressBar1.Visible = False
    
    Call ShowMemory
    Me.MousePointer = vbDefault
    
    Handler:
    End Sub
    

Anmelden zum Antworten