Java对象重用 [英] Java Object Reuse

查看:255
本文介绍了Java对象重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Objects是否应该重复使用,因为它可以重复使用?或者我们应该只在它们是重量级时才重用它,即有与之相关的操作系统资源?

Should Java Objects be reused as often as it can be reused ? Or should we reuse it only when they are "heavyweight", ie have OS resources associated with it ?

互联网上的所有旧文章都谈论对象重用和对象池为尽可能多,但我读过最近的文章说新的Object()现在已经过高度优化(10条指令),对象重用并不像以前那么大。 be。

All old articles on the internet talk about object reuse and object pooling as much as possible, but I have read recent articles that say new Object() is highly optimized now ( 10 instructions ) and Object reuse is not as big a deal as it used to be.

目前的最佳做法是什么,你们是怎么做的?

What is the current best practice and how are you people doing it ?

推荐答案

我让垃圾收集器为我做那种决定,唯一一次我用新分配的对象达到堆限制是在运行一个错误的递归算法几秒后生成3 * 27 * 27 ...新对象尽可能快。

I let the garbage collector do that kind of deciding for me, the only time I've hit heap limit with freshly allocated objects was after running a buggy recursive algorithm for a couple of seconds which generated 3 * 27 * 27... new objects as fast as it could.

做最好的可读性和封装。有时重用对象可能很有用,但通常你不必担心它。

Do what's best for readability and encapsulation. Sometimes reusing objects may be useful, but generally you shouldn't worry about it.

这篇关于Java对象重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆