`
withoutme_hw
  • 浏览: 10045 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

1.Creating and Destroying Objects2——Effective Java 2nd Ed学习笔记

 
阅读更多

Item 4: Enforce noninstantiability with a private constructor

有些类需要防止用户实例化一个类,比如说java.lang.Math 和java.util.Arrays,Attempting to enforce noninstantiability by making a class abstract does not work.A class can be made noninstantiable by including a private constructor:

Item 5: Avoid creating unnecessary objects

String s = new String("stringette"); // DON'T DO THIS!

String s = "stringette"; //DO THIS 每一次运行该代码,JVM都保证只有一个"stringette"实例

1.

2.

On my machine, the original version takes 32,000 ms for 10 million invocations, while the improved version takes 130 ms, which is about 250 times faster.

Changing the declaration of sum from Long to long reduces the runtime from 43 seconds to 6.8 seconds on my machine. The lesson is clear: prefer primitives to boxed primitives, and watch out for unintentional autoboxing.

Item 6: Eliminate obsolete object references

每次执行Stack的pop操作,Stack的大小只是在逻辑上减少了,而Object[]中对pop出来的元素的引用依然存在,所以GC不能将其回收,这样的引用被称作obsolete references,这会导致内存泄露。这样做能够避免上述情况:

但是,Joshua提醒:Nulling out object references should be the exception rather than the norm.Generally speaking, whenever a class manages its own memory, the programmer should be alert for memory leaks.Another common source of memory leaks is caches.A third common source of memory leaks is listeners and other callbacks.

Item 7: Avoid finalizers

1. Finalizers are unpredictable, often dangerous, and generally unnecessary.

2. Never depend on a finalizer to update critical persistent state

3. There is a severe performance penalty for using finalizers.

4. Explicit termination methods are typically used in combination with the try-finally construct to ensure termination.

分享到:
评论

相关推荐

    CSharp - Module 9_Creating and Destroying Objects

    在IT领域,尤其是在编程语言的学习与应用中,C#(C Sharp)作为一款由微软开发的面向对象的、类型安全的、垃圾回收的程序设计语言,其在创建和销毁对象方面的处理方式是开发者必须掌握的核心技能之一。本文将基于...

    Programming with MS VB.NET.pdf

    Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    2 Creating and Destroying Objects Item 1: Consider static factory methods instead of constructors Item 2: Consider a builder when faced with many constructor parameters Item 3: Enforce the singleton ...

    VB.Net Programming.pdf

    Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................

    MSDN Traning - VB.NET (VBL).pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    Programming with Microsoft Visual Basic.NET_Delivery Guide.pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    C++语言程序设计__期末考试试题及答案.pdf

    Destroying..1 Destroying..0 该资源摘要信息提供了一个完整的C++语言程序设计期末考试试题,涵盖了面向对象编程、函数重载、递归调用、拷贝构造函数、继承、多态性等C++编程概念,为C++语言学习者和考试者提供了一...

    C++语言程序设计第三版试卷

    - Destroying..1 - Destroying..0 程序首先创建了一个Samp类型的动态数组,然后逐个设置元素值并输出,最后释放内存。析构函数在每个对象被删除时执行,显示其内部存储的i的值。 10. Vector类是一个动态数组的...

    微软.NET程序员高级培训教程系列资料.C#(下)【英文版】

    7. **创建与销毁对象(Creating and Destroying Objects)**:"CSharp_Module 9_Creating and Destroying Objects.pdf"将涵盖对象的生命周期,包括构造函数、析构函数和垃圾回收机制,以及何时和如何适当地分配和释放...

    Unit 3 Science and nature作业题及答案解析6份3精选.docx

    3. "Instead, he thinks research efforts should concentrate ________ creating new tissues and organs that can be used to cure diseases like cancer." ——此处应填"on",意为研究应该集中在创造新的组织和...

    JAVA小实验雷电(打飞机)

    1 You will find more useful informations in the following lessons: “Java GUI”, “Java Graphics”, “Applet and Multimedia”. The lesson ‘multithreading ’ maybe is useful to some advanced ...

    C# 微软基础教程PPT

    7. **Creating and Destroying Objects**:学习如何创建和销毁对象,理解对象生命周期和垃圾回收机制。 8. **Inheritance in C#**:子类继承父类的属性和方法,实现代码重用和扩展。 9. **Aggregation, Namespaces...

    Unreal Engine 4.X By Example

    - **Trigger Volumes and Destroying Actors**: Detecting when actors should be removed. - **Debugging Our Blueprints**: Identifying and fixing issues. - **Masking Our Destruction with Particles**: ...

    TongWeb7应用开发手册.pdf

    TongWeb是东方通科技推出的一款高性能的企业级Java应用服务器,它支持最新的Servlet规范和其他Web技术,如JSP和JSF,以帮助开发者构建复杂的Web应用程序。 ### Servlet Servlet是一种Java编程接口,用于扩展服务器...

    KafkaOffsetMonitor监控工具2017年1月发布的版本

    Instead, closing and destroying the client and re-creating it. Sleeping on error before re-creating client and continuing to process Deal with thread-safety issues on shared memory between threads ...

    Msdn Training - Microsoft Visual Studio Net (With Source)

    模块9:《C# - Module 9_Creating and Destroying Objects.pdf》 本模块讲解了对象的生命周期,包括构造函数和析构函数的使用,以及垃圾回收机制。学习者将了解如何在适当的时间创建和释放对象,以优化内存管理。 ...

    django-failedfirsttestrunner:重新排序测试套件,以便在随后的测试运行中首先运行失败的测试

    django-failedfirsttestrunner ...Destroying test database for alias ' default ' ... Creating test database for alias ' default ' ... :woman_dancing::woman_dancing:.......................

    推选文档Java小程序PPT.ppt

    Java小程序的生命周期是一个重要的概念,理解它对于编写和调试Java Applet至关重要。Applet的生命周期由四个主要状态组成:初始化(Initialization)、加载(Loading)、启动(Starting)、绘画(Painting)、停止...

    Java版电子宠物游戏代码

    Java版电子宠物游戏代码,package Petgame; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Petgame extends MIDlet { private static Petgame instance; private ...

    2020高考英语 复习资料之难点讲解大全难点17 独立主格结构.doc

    1. "Weather permitting, they will go and visit the zoo." 这里"Weather permitting"就是一个独立主格结构,用来表示条件,意思是“如果天气允许的话”。其中"weather"是逻辑主语,"permitting"是现在分词,表示...

Global site tag (gtag.js) - Google Analytics