删除对象以释放内存 JAVA

huangapple go评论71阅读模式
英文:

Delete object to free memory JAVA

问题

我正在开发一个小型太空射击游戏。这是学习JAVA的有趣方式,我想确保我的操作是正确的。
这个对象(飞船)在运行时生成其他对象(激光)。
(飞船 -> 激光)。

我是这样实现的:

  • 飞船对象被绘制在屏幕上。当检测到触摸时,它会创建新的激光类实例,然后将它们添加到GameScreen类内部的静态LinkedList lasers中。
  • GameScreen对象负责绘制和更新屏幕上的所有激光、飞船和效果。

自说明。但现在:

  • 调用detectCollision(),所有与飞船对象相交的激光都会从LinkedList lasers中删除。
  • 在游戏结束时(用户退出),我会遍历存储在GameScreen.lasers链表中的所有激光类实例,并将它们全部设置为null。完成后,我还会将列表设置为null

这样释放分配的内存是正确的吗?

激光创建:

abstract class Ship {
if (canShoot()) {
    Laser laser = new Laser(new Rectangle(x, y, width, height)); // bounds
    GameScreen.playerLasers.add(laser);
}}

激光处理:

public class GameScreen {
private void dispose() {
    for (Laser laser : playerLasers) {
        laser.bounds = null;
        laser = null;
    }
    playerLasers = null;
}}

GameScreen类中的collisionDetection():

public class GameScreen {
    private void detectCollision() {
        ListIterator<Laser> itr = playerLasers.listIterator();
        while (itr.hasNext()){
            Laser laser = itr.next();
            if (laser.bounds.overlaps(ship.bounds)){
                ship.health -= laser.damage;
                laser.bounds = null;
                laser = null;
                itr.remove();
}}}}
英文:

I'm developing a small space shooter game. It's a fun way to learn JAVA and I want to make sure I'm doing things correctly.
This object (ship) is spawning other objects during runtime (lasers).
(ship -> lasers).

I've implemented things this way:

  • Object Ship is drawn on the screen. When touch is detected it's creating new instances of the class Laser then it adds them to the static LinkedList lasers inside of the GameScreen class.
  • GameScreen object is in charge of drawing and updating all the lasers, ships, and effects on the screen.

Self explanatory. But now:

  • detectCollision() is called and all the lasers that are intersecting ship objects are getting deleted from the LinkedList lasers.
  • at the end of the game (user quits) I iterate through all the instances of the Laser class that are stored in GameScreen.lasers linked list and set all of them to null. When finished I also set the list to null.

Is this the correct way of releasing allocated memory?

Laser creation:

abstract class Ship {
if (canShoot()) {
    Laser laser = new Laser(new Rectangle(x, y, width, height)); // bounds
    GameScreen.playerLasers.add(laser);
}}

Laser disposal:

public class GameScreen {
private void dispose() {
    for (Laser laser : playerLasers) {
        laser.bounds = null;
        laser = null;
    }
    playerLasers = null;
}}

GameScreen class collisionDetection():

public class GameScreen {
    private void detectCollision() {
        ListIterator&lt;Laser&gt; itr = playerLasers.listIterator();
        while (itr.hasNext()){
            Laser laser = itr.next();
            if (laser.bounds.overlaps(ship.bounds)){
                ship.health -= laser.damage;
                laser.bounds = null;
                laser = null;
                itr.remove();
}}}}

</details>


# 答案1
**得分**: 4

这不是。 

Java 是一个垃圾收集系统。你不能 'deallocate'(取消分配内存)。将东西设为 null 并不会使其被取消分配。

Java 将自动清理所需的任何内容,你无需采取任何措施来实现这一点。你唯一需要关心的是编程逻辑。例如,在这种情况下,你确实需要 `itr.remove();` 调用 - 这与取消分配 Laser 对象无关,而是与从游戏逻辑中移除 lasershot 相关。因此,移除 null 分配行,你最终会得到:

```java
if (laser.bounds.overlaps(ship.bounds)){
                ship.health -= laser.damage;
                itr.remove();
}
英文:

It's not.

Java is a garbage collected system. You can't 'deallocate'. Setting stuff to null won't make things deallocate.

Java will automatically clean up whatever is needed and you don't need to do anything to make that happen. The only thing you need to worry about is programming logic. For example, in this case, you do need the itr.remove(); call - that's not about deallocating a Laser object, that's about removing the lasershot from the game logic. Thus, remove the null alloc lines, and you end up with:

if (laser.bounds.overlaps(ship.bounds)){
                ship.health -= laser.damage;
                itr.remove();
}

huangapple
  • 本文由 发表于 2020年9月4日 20:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63740797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定