JavaFx应用程序延迟一段时间
JavaFx应用程序延迟一段时间,java,javafx,lag,resource-leak,Java,Javafx,Lag,Resource Leak,在我下面的应用程序中,红色块从屏幕顶部掉落。我的问题是,3-5分钟后,动画开始延迟,我不知道为什么。 我使用了一个探查器,发现这不是内存泄漏,但一定是资源泄漏,我非常确定,因为Cpu负载会随着时间的推移而增加。 如果您愿意,只需实现必要的类并编译和运行代码,那么您可能就会看到问题所在。(3-5分钟后) 谢谢你的帮助,祝你度过愉快的一天!:) 公共类RBYDebug扩展应用程序{ 组根=新组(); ArrayList实体=新的ArrayList(); 随机=新随机(); int sW=1600;/
在我下面的应用程序中,红色块从屏幕顶部掉落。我的问题是,3-5分钟后,动画开始延迟,我不知道为什么。
我使用了一个探查器,发现这不是内存泄漏,但一定是资源泄漏,我非常确定,因为Cpu负载会随着时间的推移而增加。
如果您愿意,只需实现必要的类并编译和运行代码,那么您可能就会看到问题所在。(3-5分钟后)
谢谢你的帮助,祝你度过愉快的一天!:)
公共类RBYDebug扩展应用程序{
组根=新组();
ArrayList实体=新的ArrayList();
随机=新随机();
int sW=1600;//屏幕宽度
int sH=980;//屏幕高度
int dy=4;//垂直移动速度
公共静态void main(字符串[]args){
应用程序启动(args);
}
公共无效开始(阶段primaryStage){
//设置实体
对于(int i=0;i<40;i++){
double x=random.nextDouble()*sW;
双y=i*50;
矩形rect=新矩形(x,-y-50,50,50);
矩形设置填充(颜色为红色);
实体。添加(rect);
}
//根组设置
root.getChildren().setAll(实体);
//设置场景
场景=新场景(根、sW、sH、Color.BLACK);
//舞台设置
primaryStage.setTitle(“RBY调试”);
初级阶段。场景(场景);
primaryStage.SetResizeable(假);
primaryStage.show();
//设置计时器
AnimationTimer=新的AnimationTimer(){
公共无效句柄(长){
更新();
}
};
timer.start();
}
公共无效更新(){
用于(矩形i:实体){
如果(i.getY()内存或资源泄漏可能会导致所描述的行为。我在您发布的代码中不太容易看到。您可以发布实体类吗?可能还有游戏行为,因为这可能是内存/资源泄漏的原因。我建议您使用Netbeans Profiler。对于内存泄漏、性能问题/调整,此工具非常有用
public class RBYDebug extends Application {
Group root = new Group();
ArrayList<Rectangle> entities = new ArrayList<>();
Random random = new Random();
int sW = 1600; //screen Width
int sH = 980; //screen Height
int dy = 4; //vertical move speed
public static void main(String[] args){
Application.launch(args);
}
public void start(Stage primaryStage){
//Setup Entities
for (int i = 0; i < 40; i++){
double x = random.nextDouble() * sW;
double y = i * 50;
Rectangle rect = new Rectangle(x, -y -50, 50, 50);
rect.setFill(Color.RED);
entities.add(rect);
}
//Root-Group setup
root.getChildren().setAll(entities);
//Setup Scene
Scene scene = new Scene(root, sW, sH, Color.BLACK);
//Stage setup
primaryStage.setTitle("RBY Debug");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
//Setup Timer
AnimationTimer timer = new AnimationTimer(){
public void handle(long now){
update();
}
};
timer.start();
}
public void update(){
for (Rectangle i : entities){
if (i.getY() <= sH){
i.setY(i.getY() + dy);
}
else {
i.setY(-i.getHeight());
}
}
root.getChildren().setAll(entities);
}
}