gpt4 book ai didi

Java 代码 (openGL) 占用过多内存并导致应用程序停止工作

转载 作者:行者123 更新时间:2023-11-30 04:20:46 31 4
gpt4 key购买 nike

我在 Java 中运行 openGL (JOGL 1.0),每隔 2 分钟后,应用程序停止工作,窗口卡住:

enter image description here

在这里你可以看到javaw.exe占用了超过1161536 KB,我认为这不正常,对吧?

那么是什么原因导致的呢?

这里是代码的核心,display()方法:

Class Renderer { ...



public Renderer ()
{
this.addGLEventListener(this);
addKeyListener(this);
addMouseListener(this);

// put the agent in a specific location inside the room
this.m_firstPersonLocation = new Point3D(750 , 200 , 5000);

this.m_coordinate1 = new Point3D(1,0,0);
this.m_coordinate2 = new Point3D(0,1,0);
this.m_coordinate3 = new Point3D(0,0,1);
this.m_center = new Point3D(0,0,0);

m_angleX = m_angleY = m_angleZ = 0;
m_xx = m_yy = m_zz = 0;

// sides of the room

this.m_sides = ROOM_GREEN;
this.m_ground = ROOM_BLUE;
this.m_roof = ROOM_BLUE;

this.m_insideCubes.add(STATIC_CUBE);
this.m_insideCubes.add(STATIC_CUBE2);
this.m_insideCubes.add(ROTATING_CUBE);
this.m_insideCubes.add(ROTATING2);
this.m_insideCubes.add(ROTATING3);
this.m_insideCubes.add(ROTATING4);
this.m_insideCubes.add(ROTATING5);
this.m_insideCubes.add(ROTATING6);
this.m_insideCubes.add(ROTATING7);
this.m_insideCubes.add(ROTATING8);
this.m_insideCubes.add(ROTATING9);
this.m_insideCubes.add(ROTATING10);
}




@Override
public void init (GLAutoDrawable gLDrawable)
{

GL gl = gLDrawable.getGL(); // get the OpenGL graphics context
gl.glEnable(GL.GL_TEXTURE_2D); // get GL Utilities
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glShadeModel(GL.GL_SMOOTH); // blends colors nicely, and smoothes out lighting

// initiate texture

gl.glEnable(GL.GL_TEXTURE_2D);
String currentCubeImage = "";

try
{

// create the actual room
m_cubicRoom = new Cube3D(ROOM_SIZE_X, ROOM_SIZE_Y, ROOM_SIZE_Z , m_sides, m_roof, m_ground);


/**
* creating elements within the 3d-room
*/

for (int i = 0; i < NUMBER_OF_3D_SHAPES; ++i)
{
// create the elements within

currentCubeImage = m_insideCubes.get(i);
m_cubes.add(new Cube3D(
CUBE_SIZE,
CUBE_SIZE,
CUBE_SIZE
,currentCubeImage,currentCubeImage,currentCubeImage));
}

}

catch (IOException e)
{
e.printStackTrace();
System.exit(0);
}


/**
* Create a cubic room
*/
m_cubicRoom.createCube(gl);

gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);


/**
* Creating m_cubes
*/
for (int i = 0; i < NUMBER_OF_3D_SHAPES; ++i)
m_cubes.get(i).createCube(gl);


/**
* Creating the listener
*/
((Component) gLDrawable).addKeyListener(this);

}


@Override
public void display(GLAutoDrawable gLDrawable)
{
moveFirstPerson();
checkCameraCollisionWithObject();

final GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();


// the point where it all begins
m_center.setPoint(m_firstPersonLocation.getX() - m_coordinate3.getX() ,
m_firstPersonLocation.getY() - m_coordinate3.getY() ,
m_firstPersonLocation.getZ() - m_coordinate3.getZ());

// the GLU look-at
m_glu.gluLookAt(m_firstPersonLocation.getX(),m_firstPersonLocation.getY(),m_firstPersonLocation.getZ(),
m_center.getX(),m_center.getY(),m_center.getZ(),
m_coordinate2.getX(),m_coordinate2.getY(),m_coordinate2.getZ());

/**
* first attache cube 1 (the room itself) - attaching the outer 3D room
*/

int shapeNumber = 0;
gl.glCallList(++shapeNumber); // cube 1

/**
* attach CUBE 2 - rotating cube
*/




/**
* debug
*/

System.out.println("--------------------------");
System.out.println("Camera current location:\n" +
"\nX location is :" + this.m_firstPersonLocation.getX() +
"\nY location is :" + this.m_firstPersonLocation.getY() +
"\nZ location is : " + this.m_firstPersonLocation.getZ());
System.out.println("***************************");


if (!collision)
{
gl.glPushMatrix();
gl.glTranslatef(400.0f, 300.0f, 1300.0f);
// gl.glRotatef(m_angleX, 4.0f, 0.0f, 0.0f);
// gl.glRotatef(m_angleY, 0.0f, 4.0f, 0.0f);
// gl.glRotatef(m_angleZ, 0.0f, 0.0f, 42.0f);
gl.glCallList(++shapeNumber); // cube 2
gl.glPopMatrix();
}





/**
* attach CUBE 3 - rotating cube
*/
gl.glPushMatrix();
gl.glTranslatef(600.0f, 350.0f, 700.0f);
gl.glRotatef(m_xx, 24.0f, 0.0f, 0.0f);
gl.glRotatef(m_yy, 0.0f, 14.0f, 0.0f);
gl.glRotatef(m_zz, 0.0f, 0.0f, 14.0f);
gl.glCallList(++shapeNumber); // cube 3
gl.glPopMatrix();

/**
* attach CUBE 4 - rotating cube
*/

gl.glPushMatrix();
gl.glTranslatef(900.0f, 350.0f , 200.0f );
gl.glRotatef(m_angleX, -2.0f, 0.0f, 0.0f);
gl.glRotatef(m_angleY, 0.0f, -1.0f, 0.0f);
gl.glRotatef(m_angleZ, 0.0f, 0.0f, -1.0f);
gl.glCallList(++shapeNumber); // cube 4

m_angleX += 0.15f;
m_angleY += 0.25f;
m_angleZ += 0.45f;
gl.glPopMatrix();


// more code
}

最佳答案

OpenGL 对象不会被垃圾回收。在 OpenGL 版本 <= 2.1 中,每次调用 glNewList(id,…)glBindTexture(id,…); ……; glTexImage2D(…) 使用之前未使用的 id 创建一个新对象。旧对象将徘徊并且不会被垃圾收集。

这很可能是您的问题:您可能不断创建新的纹理和/或显示列表而不处理旧的。

关于Java 代码 (openGL) 占用过多内存并导致应用程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17121258/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com