首页 文章 精选 留言 我的

精选列表

搜索[Android],共10000篇文章
优秀的个人博客,低调大师

Android内存优化4 了解java GC 垃圾回收机制2 GC执行finalize的过程

1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法。 finalize()与C++中的析构函数不是对应的。C++中的析构函数调用的时机是确定的(对象离开作用域或delete掉),但Java中的finalize的调用具有不确定性 不建议用finalize方法完成“非内存资源”的清理工作,但建议用于:① 清理本地对象(通过JNI创建的对象);② 作为确保某些非内存资源(如Socket、文件等)释放的一个补充:在finalize方法中显式调用其他资源释放方法。其原因可见下文[finalize的问题] 2. finalize的问题 一些与finalize相关的方法,由于一些致命的缺陷,已经被废弃了,如System.runFinalizersOnExit()方法、Runtime.runFinalizersOnExit()方法 System.gc()与System.runFinalization()方法增加了finalize方法执行的机会,但不可盲目依赖它们 Java语言规范并不保证finalize方法会被及时地执行、而且根本不会保证它们会被执行 finalize方法可能会带来性能问题。因为JVM通常在单独的低优先级线程中完成finalize的执行 对象再生问题:finalize方法中,可将待回收对象赋值给GC Roots可达的对象引用,从而达到对象再生的目的 finalize方法至多由GC执行一次(用户当然可以手动调用对象的finalize方法,但并不影响GC对finalize的行为) 3. finalize的执行过程(生命周期) (1) 首先,大致描述一下finalize流程:当对象变成(GC Roots)不可达时,GC会判断该对象是否覆盖了finalize方法,若未覆盖,则直接将其回收。否则,若对象未执行过finalize方法,将其放入F-Queue队列,由一低优先级线程执行该队列中对象的finalize方法。执行finalize方法完毕后,GC会再次判断该对象是否可达,若不可达,则进行回收,否则,对象“复活”。 (2) 具体的finalize流程: 对象可由两种状态,涉及到两类状态空间,一是终结状态空间F = {unfinalized, finalizable, finalized};二是可达状态空间R = {reachable, finalizer-reachable, unreachable}。各状态含义如下: unfinalized: 新建对象会先进入此状态,GC并未准备执行其finalize方法,因为该对象是可达的 finalizable: 表示GC可对该对象执行finalize方法,GC已检测到该对象不可达。正如前面所述,GC通过F-Queue队列和一专用线程完成finalize的执行 finalized: 表示GC已经对该对象执行过finalize方法 reachable: 表示GC Roots引用可达 finalizer-reachable(f-reachable):表示不是reachable,但可通过某个finalizable对象可达 unreachable:对象不可通过上面两种途径可达 状态变迁图: 变迁说明: 新建对象首先处于[reachable, unfinalized]状态(A) 随着程序的运行,一些引用关系会消失,导致状态变迁,从reachable状态变迁到f-reachable(B, C, D)或unreachable(E, F)状态 若JVM检测到处于unfinalized状态的对象变成f-reachable或unreachable,JVM会将其标记为finalizable状态(G,H)。若对象原处于[unreachable, unfinalized]状态,则同时将其标记为f-reachable(H)。 在某个时刻,JVM取出某个finalizable对象,将其标记为finalized并在某个线程中执行其finalize方法。由于是在活动线程中引用了该对象,该对象将变迁到(reachable, finalized)状态(K或J)。该动作将影响某些其他对象从f-reachable状态重新回到reachable状态(L, M, N) 处于finalizable状态的对象不能同时是unreahable的,由第4点可知,将对象finalizable对象标记为finalized时会由某个线程执行该对象的finalize方法,致使其变成reachable。这也是图中只有八个状态点的原因 程序员手动调用finalize方法并不会影响到上述内部标记的变化,因此JVM只会至多调用finalize一次,即使该对象“复活”也是如此。程序员手动调用多少次不影响JVM的行为 若JVM检测到finalized状态的对象变成unreachable,回收其内存(I) 若对象并未覆盖finalize方法,JVM会进行优化,直接回收对象(O) 注:System.runFinalizersOnExit()等方法可以使对象即使处于reachable状态,JVM仍对其执行finalize方法 4. 一些代码示例 (1) 对象复活 [java] view plain copy publicclasspublicstaticnullpublicstaticvoidthrowsnewnull); ifnull elsenull); ifnullelse protectedvoidthrowssuperthis} (2)覆盖finalize方法以确保资源释放 作为一个补充操作,以防用户忘记“关闭“资源,JDK中FileInputStream、FileOutputStream、Connection类均用了此”技术“,下面代码摘自FileInputStream类 [java] view plain copy /** *Ensuresthatthe<code>close</code>methodofthisfileinputstreamis *calledwhentherearenomorereferencestoit. * *@exceptionIOExceptionifanI/Oerroroccurs. *@seejava.io.FileInputStream#close() */protectedvoidthrowsifnull*willensurethatfinalizerisonlycalledwhen *safetodoso.Allreferencesusingthefdhave *becomeunreachable.Wecancallclose() */} 参考: 12.6 Finalization of Class Instanceshttps://notendur.hi.is//~snorri/SDK-docs/lang/lang083.htm 深入理解java的finalizehttp://zhang-xzhi-xjtu.iteye.com/blog/484934 The Finalizable Object http://www.artima.com/interfacedesign/Finalizable.htm 本文转自 一点点征服 博客园博客,原文链接: http://www.cnblogs.com/ldq2016/p/8469860.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

android 测试Activity,Content Provider,Service,测试些什么?(摘取自官网,个人翻译…欢迎校正)

Activity Input validation: Test that an activity responds correctly to input values in an EditText View. Set up a keystroke sequence, send it to the activity, and then usefindViewById(int)to examine the state of the View. You can verify that a valid keystroke sequence enables an OK button, while an invalid one leaves the button disabled. You can also verify that the Activity responds to invalid input by setting error messages in the View. 输入验证:测试一个activity对EditText View输入做出正确地反应.设置按键顺序,并且发送到activity中,还有使用findViewById()用来验证View的状态是否正常.你可以验证一个有效的按键序列在OK按钮上是有效的,当输入一个无效的按键会使按钮失效.你也可以验证当输入无效的设置在View中activity会做出何种反应. Lifecycle events: Test that each of your application's activities handles lifecycle events correctly. In general, lifecycle events are actions, either from the system or from the user, that trigger a callback method such asonCreate()oronClick(). For example, an activity should respond to pause or destroy events by saving its state. Remember that even a change in screen orientation causes the current activity to be destroyed, so you should test that accidental device movements don't accidentally lose the application state. 生命周期事件:测试你每一个应用程序activity 的 handles 在 生命周期里面都是正确的.通常来讲,生命周期事件都是action动作,任何来自于系统或者用户,将会触发回调方法,例如onCreate()或者onClick().举个例子,一个activity应该对pause或者destoryed做出反应来并且保存它的状态.记住,实际上屏幕方向的改变事件会导致当前activity被销毁,所以你应该测试程序在设备运行中发生意外事件时不会偶然间丢失程序的状态. Intents: Test that each activity correctly handles the intents listed in the intent filter specified in its manifest. You can useActivityInstrumentationTestCase2to send mock Intents to the activity under test. Intens:测试每一个activity对指定在manifest的intent-filter里的intents做出正确的处理.你可以使用ActivityInstrumentationTestCase2发送一个模拟Intents 到activity 在测试中 Runtime configuration changes: Test that each activity responds correctly to the possible changes in the device's configuration while your application is running. These include a change to the device's orientation, a change to the current language, and so forth. Handling these changes is described in detail in the topicHandling Runtime Changes. 改变运行时的设置:在程序运行时,测试每个activity都能对可能改变的设备配置做出正确的响应.这些改变包括改变设备的屏幕方向,改变当前语言,等等.Handing这些改变具体描述在Handling Runtime Changes. Screen sizes and resolutions: Before you publish your application, make sure to test it on all of the screen sizes and densities on which you want it to run. You can test the application on multiple sizes and densities using AVDs, or you can test your application directly on the devices that you are targeting. For more information, see the topicSupporting Multiple Screens. 屏幕的兼容性:在你发布你的程序之前,请确认测试好所有你想运行的屏幕大小.你可以使用AVDs测试你的应用程式在多种屏幕大小下,或者你可以直接测试你的程序在目标设备上.更多的信息,可以看这篇Supporting Multiple Screens. Content Provider Test with resolver methods: Even though you can instantiate a provider object inProviderTestCase2, you should always test with a resolver object using the appropriate URI. This ensures that you are testing the provider using the same interaction that a regular application would use. 测试解析方法:尽管你可以在providerTestCase2中实现一个provider,但是你应该始终用一个resolver对象测试合适的 URI.这可以确保你正在测试的provider 是在同一个应用程序中使用同样的操作. Test a public provider as a contract: If you intent your provider to be public and available to other applications, you should test it as a contract. This includes the following ideas: 测试一个公共的provider的协定:如果你想你的provider是一个公共的和可被其它程序访问的,你应该测试它的协定.这里包括了一些主意: Test with constants that your provider publicly exposes. For example, look for constants that refer to column names in one of the provider's data tables. These should always be constants publicly defined by the provider. 测试provider所有公开的常量.比如,在常量中有个列名是归类在provider’s data表中.这些常量应该始终定义在你的provider中 Test all the URIs offered by your provider. Your provider may offer several URIs, each one referring to a different aspect of the data. TheNote Padsample, for example, features a provider that offers one URI for retrieving a list of notes, another for retrieving an individual note by it's database ID, and a third for displaying notes in a live folder. 测试Provider提供的所有URIs.Provider可能提供一些URIs在data中每个都涉及到不同的方式.例如在NOTE PAD 例子中,比如,一个provider提供一个uri 用于回收一个列表的笔记记录,另外一个使用databas Id用于回收一条笔记记录,和 还有一个用于显示笔记在一个折叠面板上 Test invalid URIs: Your unit tests should deliberately call the provider with an invalid URI, and look for errors. Good provider design is to throw an IllegalArgumentException for invalid URIs. 测试无效的URIs:你的单元测试应该故意的调用provider使用一个无效的URI,并且,查看这些错误.在使用无效的URIs时,好的provider的设计是会抛出一个IllegalArgumentException异常. Test the standard provider interactions: Most providers offer six access methods: query, insert, delete, update, getType, and onCreate(). Your tests should verify that all of these methods work. These are described in more detail in the topicContent Providers. 测试标准的provider的操作.很多 providers 提供6个访问方法:查询,插入,删除,更新,获取类型,和onCreate().你的测试应该验证所有这些方法都是正常运行的.这些的详细描述可参阅Content Providers. Test business logic: Don't forget to test the business logic that your provider should enforce. Business logic includes handling of invalid values, financial or arithmetic calculations, elimination or combining of duplicates, and so forth. A content provider does not have to have business logic, because it may be implemented by activities that modify the data. If the provider does implement business logic, you should test it. 测试业务逻辑:不要忘记测试业务逻辑你的provider是可以实现的.业务逻辑包括处理无效的数据,财务或者算术的计算,消除或者联级复制,等等.一个content provider 不要有太多的业务逻辑,因为,这可能导致改变数据时部分业务逻辑被实现.如果你的provider实现了业务逻辑,你应该测试它. services Ensure that theonCreate()is called in response toContext.startService()orContext.bindService(). Similarly, you should ensure thatonDestroy()is called in response toContext.stopService(),Context.unbindService(),stopSelf(), orstopSelfResult(). 确定onCreate()方法是响应Context.startService() 或者Context.binServ()的调用.同样,你应该确定onDestoy()方法是响应Context.stopService(),Context.unbindService(),stopSelf(), orstopSelfResult(). 的调用 Test that your Service correctly handles multiple calls fromContext.startService(). Only the first call triggersService.onCreate(), but all calls trigger a call toService.onStartCommand(). In addition, remember thatstartService()calls don't nest, so a single call toContext.stopService()orService.stopSelf()(but notstopSelf(int)) will stop the Service. You should test that your Service stops at the correct point. 测试Service在Context.startService()的多次调用下正常运行.只有在第一次调用时触发Service.onCreate().但是,其它所有的调用都在Service.onStartCommand()触发.另外,记住startService()不支持嵌套调用!还有单独调用Context.stopService()或者Service.stopSelf()(但是不是stopSelf(int))将会停止Service.你应该测试你的Service在正确的点上停止. Test any business logic that your Service implements. Business logic includes checking for invalid values, financial and arithmetic calculations, and so forth. 测试Service实现的是所有业务逻辑.这些业务逻辑包括检查无效的值,财务和算术运算,等等 本文转自 liam2199 博客,原文链接:http://blog.51cto.com/youxilua/772686 如需转载请自行联系原作者

优秀的个人博客,低调大师

【读书笔记《Android游戏编程之从零开始》】15.游戏开发基础(剪切区域)

剪切区域也称为可视区域,是由画布进行设置的;它指的是在画布上设置一块区域,当画布一旦设置了可视区域,那么除此区域外,绘制的任何内容都将看不到;可视区域可以是圆形、矩形等等。 画布提供了三种设置可视区域的方法。 1.通过坐标,设置矩形可视区域 clipRect(int left,int top,int right,int bottom) 作用:为画布设置矩形可视区域 第一、二个参数:为可视区域的左上角 第三、四个参数:为可视区域的右下角2.利用 Path 来设置可视区域的形状 clipPath(Path path) 作用:为画布设置可视区域 参数:Path实例3.利用 Region 来对画布设置可视区域 clipRegion(Region region) 作用:为画布设置可视区域 参数:Region 实例 Region 表示区域的集合,所以它可以设置多个区域块,而且可以通过这些区域块之间的关系来处理一些问题;比如 Region 设置它所有区域块相交的区域是否可见、设置相交区域只让交集显示等等。 Region 常用函数: op(Rect rect,Op op) 作用:设置区域块 第一个参数:Rect 实例 第二个参数:Region.Op 静态值,表示区域块的显示方式。其中区域块的显示方式如下: Region.Op.UNION: 区域全部显示 Region.Op.INTERSECT:区域的交集显示 Region.Op.XOR:不显示交集区域 下面是实例演示,效果图如下: 创建新项目,游戏框架为 SurfaceView 游戏框架。具体步骤参照“11.游戏开发基础(SurfaceView 游戏框架、View 和 SurfaceView 的区别)”。 修改 MySurfaceView类中的绘图函数如下: private void myDraw() { try { canvas = sfh.lockCanvas(); if (canvas != null) { //通过图片资源生成一张Bitmap 位图 Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.pic01); canvas.save(); canvas.drawText("原图:", 20, 20, paint); canvas.drawBitmap(bmp, 20, 30,paint); canvas.restore(); canvas.save(); canvas.drawText("通过坐标,设置矩形可视区域:", 20, 320, paint); /** * 为画布设置矩形可视区域 * 第一、二个参数为可视区域的左上角 * 第三、四个参数为可视区域的右下角 */ canvas.clipRect(20,330,bmp.getWidth()+20,bmp.getHeight()/2+330); canvas.drawBitmap(bmp, 20, 330,paint); canvas.restore(); canvas.save(); canvas.drawText("利用 Path 来设置可视区域的形状,这里为圆形可视区域:", 20, 620, paint); Path path = new Path(); path.addCircle(20+bmp.getWidth()/2, 630+bmp.getHeight()/2, bmp.getWidth()/2, Direction.CCW); /** * 为画布设置可视区域 * 参数:Path实例 * 利用Paht 可以为位图设置任何需要的可视区域,这里是设置一个圆形可视区域。 */ canvas.clipPath(path); canvas.drawBitmap(bmp, 20, 630, paint); canvas.restore(); canvas.save(); canvas.drawText("利用 Region 来对画布设置可视区域:", 20, 920, paint); Region region = new Region(); //区域块全部显示 region.op(new Rect(20,930,120,1030), Region.Op.UNION); //不显示交集区域 region.op(new Rect(50,930,100,1080), Region.Op.XOR); canvas.clipRegion(region); canvas.drawBitmap(bmp, 20, 930, paint); canvas.restore(); } } catch (Exception e) { } finally { if (canvas != null) { sfh.unlockCanvasAndPost(canvas); } } } 本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/4164979.html,如需转载请自行联系原作者

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册