C#控制台CGI图形使用过多内存
我正在用c#进行cgi实验。我制作了一个应用程序,你可以在一个可以调整大小的矩形内移动一个正方形(带箭头)。我在一个上面画矩形来调整它的大小。我使用碰撞类来限制通过边的移动 一切似乎都很好,但我担心的是它的内存使用。当我开始增加它的大小时,内存消耗增加并触发GC 有人能帮我找到这个漏洞吗?我是不是做错了什么,导致使用率上升?我用错缓冲区了吗 事实上,我发现了导致内存使用的原因是这行代码。注释完这段代码后,我的程序只使用了16MB而不是2GBC#控制台CGI图形使用过多内存,c#,memory,console,drawing,C#,Memory,Console,Drawing,我正在用c#进行cgi实验。我制作了一个应用程序,你可以在一个可以调整大小的矩形内移动一个正方形(带箭头)。我在一个上面画矩形来调整它的大小。我使用碰撞类来限制通过边的移动 一切似乎都很好,但我担心的是它的内存使用。当我开始增加它的大小时,内存消耗增加并触发GC 有人能帮我找到这个漏洞吗?我是不是做错了什么,导致使用率上升?我用错缓冲区了吗 事实上,我发现了导致内存使用的原因是这行代码。注释完这段代码后,我的程序只使用了16MB而不是2GB if (player.rectHeight &g
if (player.rectHeight >= tempWidth)
{
Program.bufferedGraphics = context.Allocate(Program.graphics,
new Rectangle(0, 0, Convert.ToInt32(player.rectWidth) + 100,
Convert.ToInt32(player.rectHeight) + 100));
}
谢谢你抽出时间
代码
class Program
{
static Graphics graphics;
static BufferedGraphics bufferedGraphics;
static Player player;
static Collision collision;
static Utility utility;
static SByte value = 0;
static float tempWidth = 500;
static void Main()
{
Program.player = new Player();
Program.utility = new Utility();
Program.collision = new Collision();
Console.CursorVisible = false;
Process process = Process.GetCurrentProcess();
Program.graphics = Graphics.FromHdc(GetDC(process.MainWindowHandle));
BufferedGraphicsContext context = BufferedGraphicsManager.Current;
context.MaximumBuffer = new Size(Console.WindowWidth, Console.WindowHeight);
Program.bufferedGraphics = context.Allocate(Program.graphics, new Rectangle(0, 0, Convert.ToInt32(player.rectWidth),
Convert.ToInt32(player.rectHeight)));
while (true)
{
collision.worldEdges = new Collision.WorldEdges()
{
rightBorder = Convert.ToInt32(player.rectWidth),
leftBorder = 0,
topBorder = 0,
bottomBorder = Convert.ToInt32(player.rectHeight),
};
if (player.rectHeight >= tempWidth)
{
Program.bufferedGraphics = context.Allocate(Program.graphics, new Rectangle(0, 0, Convert.ToInt32(player.rectWidth) + 100,
Convert.ToInt32(player.rectHeight) + 100));
}
//Debug.Print(Convert.ToString(player.rectHeight));
// Debug.Print(Convert.ToString(player.x));
//Debug.Print(Convert.ToString(player.x- collision.worldEdges.rightBorder));
if (player.y > collision.worldEdges.bottomBorder-19.7f) // if on the edge, clamp its movement
{
player.y = collision.worldEdges.bottomBorder-19.8f;
}
if (player.x > collision.worldEdges.rightBorder - 19.7)
{
player.x = collision.worldEdges.rightBorder - 19.8f;
}
else
{
Program.player.DoMove();
}
Program.player.ResizeScreen(out value); //check whether resize the rectangle
if (value ==1) //g decrease size
{
bufferedGraphics.Graphics.FillRectangle(Brushes.Black, 0, 0,
Convert.ToInt32(player.rectWidth), Convert.ToInt32(player.rectHeight));
player.rectHeight -= 0.08f;
player.rectWidth -= 0.08f;
tempWidth = player.rectWidth;
}
if (value == -1) //h increase size
{
bufferedGraphics.Graphics.FillRectangle(Brushes.Black, 0, 0,
Convert.ToInt32(player.rectWidth), Convert.ToInt32(player.rectHeight));
player.rectHeight += 0.08f;
player.rectWidth += 0.08f;
//Program.bufferedGraphics = context.Allocate(Program.graphics, new Rectangle(0, 0, 320,200));
bufferedGraphics.Graphics.FillRectangle(Brushes.Green, 0, 0, Convert.ToInt32(player.rectWidth),
Convert.ToInt32(player.rectHeight));
tempWidth = player.rectWidth;
}
else
{
bufferedGraphics.Graphics.FillRectangle(Brushes.Green, 0, 0, Convert.ToInt32(player.rectWidth),
Convert.ToInt32(player.rectHeight));
}
Program.player.DrawPlayer(Program.bufferedGraphics.Graphics, Brushes.Blue); //draw player controlled cube
Program.bufferedGraphics.Render(Program.graphics); //finally render on screen
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern short GetKeyState(int nVirtKey);
}
class Player //detect input and set player size and it's movement
{
const int LEFT = 0x25;
const int UP = 0x26;
const int RIGHT = 0x27;
const int DOWN = 0x28;
const int G = 0x47;
public float rectWidth = 200, rectHeight = 200;
public float x;
public float y;
public Player()
{
this.x = 0;
this.y = 0;
}
public void DoMove()
{
if ((GetKeyState(LEFT) | 0x8000) > 0 && this.x < rectWidth-10)
{
this.x += 0.1f;
}
if ((GetKeyState(RIGHT) | 0x8000) > 0 && this.x > 0)
{
this.x -= 0.1f;
}
if ((GetKeyState(UP) | 0x8000) > 0 )
{
this.y += 0.1f;
}
if ((GetKeyState(DOWN) | 0x8000) > 0 && this.y > 0)
{
this.y -= 0.1f;
}
}
public void DrawPlayer(Graphics g, Brush color)
{
g.FillRectangle(color, this.x , this.y , 20, 20);
}
public void ResizeScreen(out SByte value)
{
if ((GetKeyState(G) | 0x8000) < 0)
{
value = 1;
}
else if ((GetKeyState(0x48) | 0x8000) < 0)
{
value = -1;
}
else
{
value = 0;
}
}
[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
}
class Collision //used for colliding with other objects and world boundaries
{
public struct WorldEdges
{
public int leftBorder;
public int rightBorder;
public int topBorder;
public int bottomBorder;
}
public WorldEdges worldEdges;
}
类程序
{
静态图形;
静态缓冲图形缓冲图形;
静态播放器;
静态碰撞;
静态效用;
静态SByte值=0;
静态浮动宽度=500;
静态void Main()
{
Program.player=新玩家();
Program.utility=new utility();
Program.collision=新的碰撞();
Console.CursorVisible=false;
Process=Process.GetCurrentProcess();
Program.graphics=graphics.FromHdc(GetDC(process.MainWindowHandle));
BufferedGraphicsContext上下文=BufferedGraphicsManager.Current;
context.MaximumBuffer=新尺寸(Console.WindowWidth,Console.WindowHeight);
Program.bufferedGraphics=context.Allocate(Program.graphics,新矩形(0,0,Convert.ToInt32(player.rectWidth)),
转换为32(player.rectHeight));
while(true)
{
collision.worldEdge=新建碰撞.worldEdge()
{
rightBorder=Convert.ToInt32(player.rectWidth),
leftBorder=0,
topBorder=0,
bottomBorder=Convert.ToInt32(player.rectHeight),
};
如果(player.rectHeight>=tempWidth)
{
Program.bufferedGraphics=context.Allocate(Program.graphics,新矩形(0,0,Convert.ToInt32(player.rectWidth)+100,
转换为32(player.rectHeight)+100);
}
//Debug.Print(Convert.ToString(player.rectHeight));
//Debug.Print(Convert.ToString(player.x));
//打印(Convert.ToString(player.x-collision.worldEdge.rightBorder));
if(player.y>collision.worldEdge.bottomBorder-19.7f)//如果在边缘上,则钳制其移动
{
player.y=collision.worldwedges.bottomBorder-19.8f;
}
如果(player.x>collision.worldEdge.rightBorder-19.7)
{
player.x=collision.worldEdge.rightBorder-19.8f;
}
其他的
{
Program.player.DoMove();
}
Program.player.ResizeScreen(输出值);//检查是否调整矩形的大小
如果(值==1)//g减小大小
{
缓冲图形。图形。填充矩形(画笔。黑色,0,0,
Convert.ToInt32(player.rectWidth),Convert.ToInt32(player.rectHeight));
player.rect高度-=0.08f;
player.rectWidth-=0.08f;
tempWidth=player.rectWidth;
}
如果(值==-1)//h增加大小
{
缓冲图形。图形。填充矩形(画笔。黑色,0,0,
Convert.ToInt32(player.rectWidth),Convert.ToInt32(player.rectHeight));
player.rect高度+=0.08f;
player.rectWidth+=0.08f;
//Program.bufferedGraphics=context.Allocate(Program.graphics,新矩形(0,032200));
bufferedGraphics.Graphics.FillRectangle(Brush.Green,0,0,Convert.ToInt32(player.rectWidth),
转换为32(player.rectHeight));
tempWidth=player.rectWidth;
}
其他的
{
bufferedGraphics.Graphics.FillRectangle(Brush.Green,0,0,Convert.ToInt32(player.rectWidth),
转换为32(player.rectHeight));
}
Program.player.DrawPlayer(Program.bufferedGraphics.Graphics,Brush.Blue);//绘制播放器控制的立方体
Program.bufferedGraphics.Render(Program.graphics);//最终在屏幕上渲染
}
}
[DllImport(“user32.dll”,CharSet=CharSet.Auto)]
公共静态外部IntPtr GetDC(IntPtr hWnd);
[DllImport(“user32.dll”,CharSet=CharSet.Auto)]
静态外部短GetKeyState(int nVirtKey);
}
类Player//检测输入并设置播放器大小及其移动
{
常量int LEFT=0x25;
常数int UP=0x26;
const int RIGHT=0x27;
常数int DOWN=0x28;
常数int G=0x47;
公共浮动宽度=200,矩形高度=200;
公共浮动x;
公众浮躁;
公共玩家()
{
这个.x=0;
这个。y=0;
}
公共空间
{
if((GetKeyState(左)| 0x8000)>0&&this.x0&&this.x>0)
{
这是0.x-=0.1f;
}
如果((GetKeyState(向上)| 0x8000)>0)
{
这是0.y+=0.1f;
}
if((GetKeyState(向下)| 0x8000)>0&&this.y>0)
{
这.y-=0.1f;
}
}
public void DrawPlayer(图形g,画笔颜色)
{
g、 圆角矩形(颜色,this.x,this.y,20,20);
}
公共无效大小屏幕(输出SByte值)
{
如果((GetKeyState(G)| 0x8000)<0)
{
数值=1;
}
否则如果((GetKeyState(0x48)| 0x8000)<0)
{
值=-1;
}
其他的
{
数值=0;
}
}
[DllImport(“user32.dll”)]