java慎用String.substring(int start, int end)

2024-06-07 20:38

本文主要是介绍java慎用String.substring(int start, int end),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1:问题的抛出

        今天在安卓项目中使用后台线程操作一个大文件,分块读取文件中的所有内容,每次操作加载一个小块进行解析,解析到指定的文本内容之后会加载并常驻内存中,即使所有我解析到的文本内容被加入到内存中也不会很大,这样不会造成内存泄露问题。原理如此,但是最终依然出现oom。

2:问题的排查

仔细检查之后发现线程中所有的产生的对象都已经在操作结束之后回收(即:生成的对象没有引用)。代码很简短,挨个排查,发现一个string对象调用了substring(int start, int end)方法,查看了一下substring方法的注释:

Returns a string containing a subsequence of characters from this string. The returned string shares this string's backing array.

注释中的这句话"shares this string's backing array"让我产生一个疑问:什么是backing array,会不会是原来string的所有内容数组?带着这个问题,我把调用该方法的代码注释掉再次运行,哈哈,果然顺畅了。

3:产生的原因

定位到String.substring(int start, int end)的源代码:

public String substring(int start, int end) {if (start == 0 && end == count) {return this;}// NOTE last character not copied!// Fast range check.if (start >= 0 && start <= end && end <= count) {return new String(offset + start, end - start, value);}throw startEndAndLength(start, end);}

可见,正常情况下返回的是new String(offset + start, end - start, value);这个新的字符串,重点看看那个value参数到底是什么呢,源码中定义为:

private final char[] value;

我在构造自己的String的时候使用的是 String (byte[] data)构造方法,追溯到value数组初始化的地方:

 public String(byte[] data, int offset, int byteCount, Charset charset) {if ((offset | byteCount) < 0 || byteCount > data.length - offset) {throw failedBoundsCheck(data.length, offset, byteCount);}// We inline UTF-8, ISO-8859-1, and US-ASCII decoders for speed and because 'count' and// 'value' are final.String canonicalCharsetName = charset.name();if (canonicalCharsetName.equals("UTF-8")) {byte[] d = data;char[] v = new char[byteCount];int idx = offset;int last = offset + byteCount;int s = 0;
outer:while (idx < last) {byte b0 = d[idx++];if ((b0 & 0x80) == 0) {// 0xxxxxxx// Range:  U-00000000 - U-0000007Fint val = b0 & 0xff;v[s++] = (char) val;} else if (((b0 & 0xe0) == 0xc0) || ((b0 & 0xf0) == 0xe0) ||((b0 & 0xf8) == 0xf0) || ((b0 & 0xfc) == 0xf8) || ((b0 & 0xfe) == 0xfc)) {int utfCount = 1;if ((b0 & 0xf0) == 0xe0) utfCount = 2;else if ((b0 & 0xf8) == 0xf0) utfCount = 3;else if ((b0 & 0xfc) == 0xf8) utfCount = 4;else if ((b0 & 0xfe) == 0xfc) utfCount = 5;// 110xxxxx (10xxxxxx)+// Range:  U-00000080 - U-000007FF (count == 1)// Range:  U-00000800 - U-0000FFFF (count == 2)// Range:  U-00010000 - U-001FFFFF (count == 3)// Range:  U-00200000 - U-03FFFFFF (count == 4)// Range:  U-04000000 - U-7FFFFFFF (count == 5)if (idx + utfCount > last) {v[s++] = REPLACEMENT_CHAR;continue;}// Extract usable bits from b0int val = b0 & (0x1f >> (utfCount - 1));for (int i = 0; i < utfCount; ++i) {byte b = d[idx++];if ((b & 0xc0) != 0x80) {v[s++] = REPLACEMENT_CHAR;idx--; // Put the input char backcontinue outer;}// Push new bits in from the right sideval <<= 6;val |= b & 0x3f;}// Note: Java allows overlong char// specifications To disallow, check that val// is greater than or equal to the minimum// value for each count://// count    min value// -----   ----------//   1           0x80//   2          0x800//   3        0x10000//   4       0x200000//   5      0x4000000// Allow surrogate values (0xD800 - 0xDFFF) to// be specified using 3-byte UTF values onlyif ((utfCount != 2) && (val >= 0xD800) && (val <= 0xDFFF)) {v[s++] = REPLACEMENT_CHAR;continue;}// Reject chars greater than the Unicode maximum of U+10FFFF.if (val > 0x10FFFF) {v[s++] = REPLACEMENT_CHAR;continue;}// Encode chars from U+10000 up as surrogate pairsif (val < 0x10000) {v[s++] = (char) val;} else {int x = val & 0xffff;int u = (val >> 16) & 0x1f;int w = (u - 1) & 0xffff;int hi = 0xd800 | (w << 6) | (x >> 10);int lo = 0xdc00 | (x & 0x3ff);v[s++] = (char) hi;v[s++] = (char) lo;}} else {// Illegal values 0x8*, 0x9*, 0xa*, 0xb*, 0xfd-0xffv[s++] = REPLACEMENT_CHAR;}}if (s == byteCount) {// We guessed right, so we can use our temporary array as-is.this.offset = 0;this.value = v;this.count = s;} else {// Our temporary array was too big, so reallocate and copy.this.offset = 0;this.value = new char[s];this.count = s;System.arraycopy(v, 0, value, 0, s);}} else if (canonicalCharsetName.equals("ISO-8859-1")) {this.offset = 0;this.value = new char[byteCount];this.count = byteCount;Charsets.isoLatin1BytesToChars(data, offset, byteCount, value);} else if (canonicalCharsetName.equals("US-ASCII")) {this.offset = 0;this.value = new char[byteCount];this.count = byteCount;Charsets.asciiBytesToChars(data, offset, byteCount, value);} else {CharBuffer cb = charset.decode(ByteBuffer.wrap(data, offset, byteCount));this.offset = 0;this.count = cb.length();if (count > 0) {// We could use cb.array() directly, but that would mean we'd have to trust// the CharsetDecoder doesn't hang on to the CharBuffer and mutate it later,// which would break String's immutability guarantee. It would also tend to// mean that we'd be wasting memory because CharsetDecoder doesn't trim the// array. So we copy.this.value = new char[count];System.arraycopy(cb.array(), 0, value, 0, count);} else {this.value = EmptyArray.CHAR;}}}

看看源码就终于明白了,value长度就是原有byte数组根据不同编码计算得到的结果,其内容自然是字符串中所有数据内容。

4:总结

java中的String.substring(int start, int end)方法返回的新字符串仍然保持原来字符串的数据引用,如果数据量比较大,这里需要注意一下会不会产生内存问题。

这篇关于java慎用String.substring(int start, int end)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1040243

相关文章

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2