-
Feb 20th, 2016, 09:04 PM
#1
Thread Starter
Addicted Member
How to Reduce Process Memory? *Solved*
Hey, I've been trying to make a game using pure VB, and obviously one must use many graphics objects. This is just test code, but still, when I debug the program, the memory still goes up to 600-700MB. Graphics objects are created and used very fast as you can see. How can I reduce this? Do I need to dispose of them after being used?
Thanks!
~Nic
vb Code:
Public Class Form1
Dim DefaultImg As New Bitmap(My.Resources.TestImg)
Dim T1 As New Timer() With {.Interval = 10, .Enabled = True}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DoubleBuffered = True
AddHandler T1.Tick, AddressOf T1_Tick
End Sub
Private Function LotOfCode() As Bitmap
Dim Bmp As New Bitmap(800, 600)
Dim G As Graphics = Graphics.FromImage(Bmp)
G.DrawImage(DefaultImg, -5, -5)
G.FillRectangle(Brushes.Red, 5, 5, 15, 15)
Return Bmp
End Function
Private Sub T1_Tick()
Invalidate()
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
Dim TestImg As Bitmap = LotOfCode()
Dim ImgPt As New Point(25, 25)
e.Graphics.DrawImage(TestImg, ImgPt)
End Sub
End Class
Last edited by NinjaNic; Feb 21st, 2016 at 01:08 AM.
-
Feb 20th, 2016, 10:37 PM
#2
Thread Starter
Addicted Member
Re: How to Reduce Process Memory?
Here's some more info: My real game/program/thing actually gets up to 2 GB before it crashes. (That's the max limit for debugging, right?) And there's also a lot of math used repeatedly, but I wouldn't expect that to be a problem. I just read about the "Using" statement, so I tried it out in my test program but it didn't change anything, unless I did it wrong. Is it common, or even preferred to use Graphics in a "Using" block?
Thanks.
-
Feb 21st, 2016, 01:10 AM
#3
Thread Starter
Addicted Member
Re: How to Reduce Process Memory? *Solved*
Solution:
Put all the code into Form1_Paint's sub procedure. It speeds up the app by a lot, plus it doesn't use much memory. I'm not sure why.
-
Feb 21st, 2016, 08:33 AM
#4
Re: How to Reduce Process Memory? *Solved*
Whatever caused it is in your real code, that example doesn't have anything that should cause additional memory usage or slowdown. It's likely there's some difference between your example and the real code that didn't show through.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|