Lot Memory usage when write mass Integer and String into one file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi all, I was create game music server, the game need file that contains list of music, the file called "OJNList.dat", and OJNList will be loaded when user click select music in game play.
My program was working and generated working OJNList (the structure offset doesnt wrong), but the write performance very badly...
Program is contain the listview with details style, and have 9 column, the column 1 - 8 is contains meta data of song like Title of song, artist of song and etc, column 9 is contain location of song (if song inside ojnlist, the column item will be ojnlist.dat,
but if song is added from file and song isnt written on ojnlist, the item will be path of song (ex: C:test.ojn))
and I want when user hit Save button, the program will create temp of ojnlist, next program will check item per item (using loop) item in column 9 is exist or not, if exist, program will be read first 300 bytes in that file (in item string in column 9) and
write into ojnlist (not temped ojnlist), and when column 9 is not exist then program try read first byte (offset 0 that contains Signature (like number sign of song) in integer 32 bits) of temped ojnlist and then try to search item in the listview that same
like sign of song is exist or not to make sure removed song isnt write in saved ojnlist, if exist, read from start offset (offset is 4) 300 byte from temped ojnlist, and write it into ojnlist, if not exist that mean song is removed so do nothing, then after
that start offset + 300 to read next song from temped ojnlist, start offset is begin from 4, so next song is 304, 604, 904 and so on until loop is end..
and here my code (ojnlistview is listview and ojnlistpathbox is textbox that contains text location of ojnlist that opened / created):

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Sub BuildOJNList()
<span style="color:Blue; Try
OriginalFilename = Form1.OJNListPathbox.Text
<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OriginalFilename & <span style="color:#A31515; ".opttemp", FileMode.Create)
<span style="color:Blue; Dim OJNListDatasXByteS = File.ReadAllBytes(OriginalFilename)
fs.Write(OJNListDatasXByteS, 0, OJNListDatasXByteS.Length)
<span style="color:Blue; End <span style="color:Blue; Using
Form1.OJNListPathbox.Text = OriginalFilename & <span style="color:#A31515; ".opttemp"
<span style="color:Blue; Dim list = Form1.OJNListView
<span style="color:Blue; Dim OJNListPath = Form1.SaveOJNList.FileName
<span style="color:Blue; Dim OJNList = Form1.OJNListView
<span style="color:Blue; Dim TotalSong = Form1.OJNListView.Items.<span style="color:Blue; Count
<span style="color:Blue; Dim CurrentIndex <span style="color:Blue; As <span style="color:Blue; Integer = 0
Loadbar.Maximum = TotalSong
OJNList.FocusedItem = OJNList.TopItem

<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OJNListPath, FileMode.Create)
<span style="color:Blue; Dim EmptyByte(304) <span style="color:Blue; As <span style="color:Blue; Byte
fs.Write(EmptyByte, 0, EmptyByte.Length)
<span style="color:Blue; End <span style="color:Blue; Using

<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OJNListPath, FileMode.Open)
<span style="color:Blue; Using bw <span style="color:Blue; As <span style="color:Blue; New BinaryWriter(fs)
fs.Seek(0, SeekOrigin.Begin)
<span style="color:Blue; Dim OJNCount <span style="color:Blue; As Int32 = <span style="color:Blue; CInt(TotalSong)
bw.Write(OJNCount)
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; End <span style="color:Blue; Using

<span style="color:Blue; For Value <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To TotalSong - 1

<span style="color:Blue; If File.Exists(OJNList.Items(CurrentIndex).SubItems(9).Text) = <span style="color:Blue; True <span style="color:Blue; Then
<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OJNList.Items(CurrentIndex).SubItems(9).Text, FileMode.Open)
<span style="color:Blue; Using rdr <span style="color:Blue; As <span style="color:Blue; New BinaryReader(fs)
fs.Seek(108, SeekOrigin.Begin)
<span style="color:Blue; Dim tempx = rdr.ReadBytes(32)
<span style="color:Blue; Dim Title = Encoding.UTF8.GetString(tempx)
lblloadstat.Text = <span style="color:#A31515; "Processing - Adding Song: " & Title
Refresh()
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; End <span style="color:Blue; Using

<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OJNList.Items(CurrentIndex).SubItems(9).Text, FileMode.Open)
<span style="color:Blue; Using rdr <span style="color:Blue; As <span style="color:Blue; New BinaryReader(fs)
fs.Seek(0, SeekOrigin.Begin)
MetaByte = rdr.ReadBytes(300)
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; End <span style="color:Blue; Using

<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OJNListPath, FileMode.Open)
fs.Seek(StartOff, SeekOrigin.Begin)
fs.Write(MetaByte, 0, MetaByte.Length)
<span style="color:Blue; End <span style="color:Blue; Using
OJNList.Items(CurrentIndex).SubItems(9).Text = Path.GetFileName(OJNListPath)
FileAdded += 1
<span style="color:Blue; Else
<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OriginalFilename & <span style="color:#A31515; ".opttemp", FileMode.Open)
<span style="color:Blue; Using rdr <span style="color:Blue; As <span style="color:Blue; New BinaryReader(fs)
fs.Seek(StartOff, SeekOrigin.Begin)
ID = rdr.ReadInt32
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; End <span style="color:Blue; Using

<span style="color:Blue; Dim liToFind
<span style="color:Blue; For <span style="color:Blue; Each li <span style="color:Blue; In OJNList.Items
liToFind = OJNList.FindItemWithText(ID)
<span style="color:Blue; Next

<span style="color:Blue; If <span style="color:Blue; Not IsNothing(liToFind) <span style="color:Blue; Then
<span style="color:Blue; Dim TitleOff <span style="color:Blue; As <span style="color:Blue; Integer = StartOff + 108

<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OriginalFilename & <span style="color:#A31515; ".opttemp", FileMode.Open)
<span style="color:Blue; Using rdr <span style="color:Blue; As <span style="color:Blue; New BinaryReader(fs)
fs.Seek(TitleOff, SeekOrigin.Begin)
<span style="color:Blue; Dim tempx = rdr.ReadBytes(32)
<span style="color:Blue; Dim Title = Encoding.UTF8.GetString(tempx)
lblloadstat.Text = <span style="color:#A31515; "Processing: " & Title
Refresh()
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; End <span style="color:Blue; Using

Refresh()
<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(Form1.OJNListPathbox.Text, FileMode.Open)
<span style="color:Blue; Using rdr <span style="color:Blue; As <span style="color:Blue; New BinaryReader(fs)
fs.Seek(StartOff, SeekOrigin.Begin)
MetaByte = rdr.ReadBytes(300)
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; End <span style="color:Blue; Using

<span style="color:Blue; Using fs <span style="color:Blue; As <span style="color:Blue; New FileStream(OJNListPath, FileMode.Open)
fs.Seek(StartOff, SeekOrigin.Begin)
fs.Write(MetaByte, 0, MetaByte.Length)
<span style="color:Blue; End <span style="color:Blue; Using
<span style="color:Blue; ElseIf IsNothing(liToFind) <span style="color:Blue; Then
StartOff += 300
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If


Loadbar.Value += 1
CurrentIndex += 1
StartOff += 300
<span style="color:Blue; Next
Form1.OJNListPathbox.Text = OriginalFilename
File.Delete(OriginalFilename & <span style="color:#A31515; ".opttemp")
lblloadstat.Text = <span style="color:#A31515; "Completed."
lblloadstat.Text = <span style="color:#A31515; "File Successfully Saved."
Loadbar.Value = Loadbar.Maximum
Form1.RefreshListToolStripMenuItem.Enabled = <span style="color:Blue; True
StartOff = 4
MsgBox(FileAdded & <span style="color:#A31515; " Meta Data added into File" & ControlChars.CrLf & _
Form1.lblremoved.Text & <span style="color:#A31515; " Meta Data Has been removed from File." & ControlChars.CrLf & _
<span style="color:#A31515; "All data was successfully Saved.", vbInformation, <span style="color:#A31515; "Info")
Form1.lblremoved.Text = 0
FileAdded = 0
Form1.OJNListPathbox.Text = OJNListPath
Form1.OJNListView.Refresh()
<span style="color:Blue; Me.Dispose()
<span style="color:Blue; Me.Close()

<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
MsgBox(<span style="color:#A31515; "Error When Rebuilding File: " & ControlChars.CrLf & _
ex.Message, vbCritical, <span style="color:#A31515; "Error")
MsgBox(<span style="color:#A31515; "All Info Was not damaged. and " & FileAdded & <span style="color:#A31515; " Meta Data added into File" & ControlChars.CrLf & _
Form1.lblremoved.Text & <span style="color:#A31515; "Meta Data Has been removed from File.")
Form1.OJNListView.Refresh()
<span style="color:Blue; Me.Close()
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
[/code]

<br/>
the problem is the when user trying save in second time, the performance is very low and lot using memory, and program will be not responding, but the first time save isnt good too but its enough I think.
this only appear when I try save with song more than 450 song, (by default its contains 875 song or more), if i try save with little song, the performance doesnt have problem
Sorry for my English so bad...
Thanks!

View the full article
 
Back
Top