stringbuilder

Memory Leak in StringBuilder when using Insert() and Clear()

泄露秘密 提交于 2019-12-29 00:40:31
问题 I need to add some lines to a StringBuilder, where the line added last should be at the beginning of the string and the oldest at the end. I add a new line like this: stringBuilder.Insert(0, "Some text." + Environment.NewLine); Once done, I empty the StringBuilder like this: stringBuilder.Clear(); I reuse the same StringBuilder many times, which leads to an Out of Memory exception over time. I investigated the problem with the test program below, which shows that when using Append() and Clear

Java 高效代码50例

时光总嘲笑我的痴心妄想 提交于 2019-12-28 17:33:53
导读   世界上只有两种物质:高效率和低效率;世界上只有两种人:高效率的人和低效率的人。----萧伯纳 常量&变量 直接赋值常量,禁止声明新对象   直接赋值常量值,只是创建了一个对象引用,而这个对象引用指向常量值。 反例 Long i= new Long(1L ); String s = new String("abc"); 正例 Long i=1L ; String s ="abc"; 当成员变量值无需改变时,尽量定义为静态常量   在类的每个对象实例中,每个成员变量都有一份副本,而成员静态常量只有一份实例。 反例 public class HttpConnection{ private final long timeout=5L ; ... } 正例 public class HttpConnection{ private static final long timeout=5L ; ... } 尽量使用基本数据类型,避免自动装箱和拆箱   Java中的基本数据类型double、float、long、int、short、char、boolean,分别对应包装类Double、Float、Long、Integer、Short、Character、Boolean。   Jvm支持基本类型与对象包装类的自动转换,被称为自动装箱和拆箱。装箱和拆箱都是需要CPU和内存资源的

2019最新整理JAVA面试题附答案

笑着哭i 提交于 2019-12-28 15:37:08
本人免费整理了Java高级资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共30G,需要自己领取。 传送门: https://mp.weixin.qq.com/s/igMojff-bbmQ6irCGO3mqA 包含的模块: 本文分为十九个模块,分别是:Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM 如下图所示: 共包含 208 道面试题,本文的宗旨是为读者朋友们整理一份详实而又权威的面试清单,下面一起进入主题吧。 ==================================================== 一. Java 基础模块 1.JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,Java 开发工具包,提供了 Java 的开发环境和运行环境。 JRE:Java Runtime Environment 的简称,Java 运行环境,为 Java 的运行提供了所需环境。 具体来说 JDK 其实包含了

StringBuilder for string concatenation throws OutOfMemoryException

浪尽此生 提交于 2019-12-28 06:28:14
问题 We mostly tend to following the above best practice. Have a look at String vs StringBuilder But StringBuilder could throw OutOfMemoryException even when there is sufficient memory available . It throws OOM exception because it needs "continuous block of memory". Some links for reference StringBuilder OutOfMemoryException and there are many more..... How many of you faced this problem or aware and what did you do to resolve it? Is there anything I am missing? P.S: I wasn't aware of this. I

Java 驼峰转为下划线

本小妞迷上赌 提交于 2019-12-27 20:30:56
/** * 驼峰转为下划线 * @return */ public static String turnCaseToLine(String tuofeng) { if (null != tuofeng) { StringBuilder current = new StringBuilder(100); char[] currentChar = tuofeng.toCharArray(); for (int s = 0; s < currentChar.length; s++) { if (Character.isUpperCase(currentChar[s])) { // 大写 current.append("_").append(String.valueOf(currentChar[s]).toLowerCase()); } else { current.append(currentChar[s]); } } return current.toString(); } return null; } 来源: CSDN 作者: Fantacy. 链接: https://blog.csdn.net/Little_Stars/article/details/103737032

基础——StringBuilder类学习

无人久伴 提交于 2019-12-27 12:31:51
string和stringbuilder的区别 string对象是不可变的,stringbuilder是可变的。使用string创建新对象时都需要为该对象分配空间。当需要对字符串进行重复修改时就需要用到stringbulider类。 代码 1 StringBuilder sb = new StringBuilder("ni hao"); //声明一个stringbuilder2 sb.Append(" conn"); //修改sb的大小,在sb的后面追加字符串“ conn”,此时的sb为“ni hao conn”3 sb.Capacity = 5; //设置sb的长度,此时sb为“ni ha” 来源: https://www.cnblogs.com/donliu/archive/2010/03/18/1689400.html

动态配置多数据源单一数据源,按库名,表名,实体类操作数据

只谈情不闲聊 提交于 2019-12-27 03:33:11
maven: <!----> <!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml --> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/org.springframework.jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>org.springframework.jdbc</artifactId> <version>3.2

汉字转GBk

点点圈 提交于 2019-12-27 02:33:38
//汉字转GBK public static String toGBK(String source) throws UnsupportedEncodingException { StringBuilder sb = new StringBuilder(); byte[] bytes = source.getBytes("GBK"); for(byte b : bytes) { sb.append("%" + Integer.toHexString((b & 0xff)).toUpperCase()); } return sb.toString(); } 来源: CSDN 作者: loading.... 链接: https://blog.csdn.net/qq_40996012/article/details/103712291

Android Lint 代码提示之: StringBuilder 替换 StringBuffer

主宰稳场 提交于 2019-12-26 04:22:26
StringBuilder StringBuffer是Java的基础。 今天在Android lint代码的时候,发现了修改提示。记录下,作为总结。先看代码段: byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8); byte[] md5Bytes = md5.digest(byteArray); //下面这个hexValue,会提示建议改成StringBuilder类型 StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); 如上代码中的注释,建议改成StringBuilder。为何建议改成StringBuilder呢? 提示信息意思是:报告指出 所有被定义为java.lang.StringBuffer的变量,如果被定义为java.lang.StringBuilder ,应该是更加高效。java.lang

Why StringBuilder.Replace is slower than String.Replace

Deadly 提交于 2019-12-25 18:36:34
问题 According to the following unit test methods, StringBuilder is far slower than String.Replace, how come every one saying StringBuilder is faster? Am I missing something? [TestMethod] public void StringReplace() { DateTime date = DateTime.Now; string template = File.ReadAllText("file.txt"); for (int i = 0; i < 100000; i++) { template = template.Replace("cat", "book" ); template = template.Replace("book", "cat"); } Assert.Fail((DateTime.Now - date).Milliseconds.ToString()); } [TestMethod]