我尝试查询本地应用程序事件日志并返回所有条目的一些详细信息。我读了这么多条目后,我遇到了一个内存异常(见下文)。任何人都可以协助我如何调整内存,以便读取所有条目而不会失败?我的应用程序事件日志包含大约16,000个条目。依赖于我本地工作站上的可用内存吗?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import com.sun.jna.*;
import com.sun.jna.Library.Handler;
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.Advapi32Util.*;
import com.sun.jna.platform.win32.WinNT.*;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.*;
public class UPDATEDtest {
public static void main(String[] args) throws NumberFormatException, IOException {
boolean Found = false;
HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
IntByReference pnBytesRead = new IntByReference();
IntByReference pnMinNumberOfBytesNeeded = new IntByReference();
int bufSize = 0x7ffff; //(r.size()) * 2048;
Memory buffer = new Memory(bufSize);
IntByReference pOldestRecord = new IntByReference();
assertTrue(Advapi32.INSTANCE.GetOldestEventLogRecord(h, pOldestRecord));
int dwRecord = pOldestRecord.getValue();
int rc = 0;
while(true) {
if (! Advapi32.INSTANCE.ReadEventLog(h,
WinNT.EVENTLOG_SEQUENTIAL_READ | WinNT.EVENTLOG_FORWARDS_READ,
0, buffer, (int) buffer.size(), pnBytesRead, pnMinNumberOfBytesNeeded))
{
rc = Kernel32.INSTANCE.GetLastError();
if (rc == W32Errors.ERROR_INSUFFICIENT_BUFFER) {
buffer = new Memory(pnMinNumberOfBytesNeeded.getValue());
continue;
}
break;
}
int dwRead = pnBytesRead.getValue();
Pointer pevlr = buffer;
while (dwRead > 0)
{
EVENTLOGRECORD record = new EVENTLOGRECORD(pevlr);
dwRead -= record.Length.intValue();
pevlr = pevlr.share(record.Length.intValue());
System.out.println("Event ID: " + record.EventID.shortValue());
System.out.println("Event Record Number: " + record.RecordNumber);
System.out.println("Event Time Written: " + record.TimeWritten);
System.out.println("Event Source: " + pevlr.getString(record.size(), true));
}
}
assertTrue(rc == W32Errors.ERROR_HANDLE_EOF);
assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
}
private static void assertTrue(boolean getOldestEventLogRecord) {
}
}
以下是返回结果的一部分,然后异常开始。
Event ID: 1704
Event Record Number: 3326
Event Time Written: 1335280438
Event Source: MsiInstaller
Event ID: 1040
Event Record Number: 3327
Event Time Written: 1335280463
Event Source: Microsoft-Windows-RestartManager
Exception in thread "main" java.lang.IndexOutOfBoundsException: Bounds exceeds available space : size=524287, offset=524296
at com.sun.jna.Memory.boundsCheck(Memory.java:185)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory$SharedMemory.boundsCheck(Memory.java:62)
at com.sun.jna.Memory $ SharedMemory.boundsCheck(Memory.java:62)一直在继续