Nodejs 分析:父级(切片字符串) [英] Nodejs profiling: parent in (sliced string)

查看:33
本文介绍了Nodejs 分析:父级(切片字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分析了我的 nodejs 应用程序并看到了一些奇怪的字符串,它被标记为 parent in (sliced string).

I profiled my nodejs application and see some strange strings, it's marked as parent in (sliced string).

我使用 v8-profiler 和 node-inspector.

I using v8-profiler and node-inspector.

这是分析器屏幕的照片(抱歉照片,不是截图,只是我的系统真的超载并且没有足够的空间来制作屏幕):http://i.imgur.com/dkkPbGA.jpg

Here is photo of profiler screen (sorry for photo, not screenshot, just my system really overloaded and don't have enough space to make screen): http://i.imgur.com/dkkPbGA.jpg

那么,我的问题是,(切片字符串) 字符串中的 父级是什么?

So, my question, what is this parent in (sliced string) strings?

UPD:经过一些审查看起来我明白了,当我对字符串进行切片时,它存储了父字符串(这是优化吗?).结果这个父字符串在内存中.在我复制了这个拼接字符串之后,看起来这个父字符串已经从内存中清除了.我说得对吗?

UPD: After some review looks like i understand, when i made slice on string, it's stored parent string (is it optimization?). In result this parents strings was in memory. After i copied this spliced strings, looks like there this parent strings was cleaned from memory. Am i right?

推荐答案

是的,这是一种优化.当您有一个长字符串 var longstring = "abc...",并且您从中创建一个子字符串(又名切片")时 var short = longstring.substr(20, 30),那么字符实际上并没有在内存中复制;相反,short 在内部仅表示为 |longstring| 的一个切片.从索引 20 开始,长度为 30longstring 被称为 short 的父".只要您的代码保持对 short 的引用,垃圾收集器就无法释放 longstring.

Yes, it's an optimization. When you have a long string var longstring = "abc...", and you create a substring (a.k.a. "slice") from it var short = longstring.substr(20, 30), then the characters are not actually copied around in memory; instead short is internally just represented as a slice of |longstring| beginning at index 20 with a length of 30, and longstring is called short's "parent". As long as your code keeps a reference to short, longstring cannot be freed by the garbage collector.

(作为记录,我们(V8 开发人员)知道这种优化会对内存消耗产生负面影响,因为它会导致长字符串保持活动状态,而只复制出仍然需要的部分会使用更少的内存.问题是,在一般情况下,计算该操作是否有意义(因为仍然只需要一个短块)或没有(因为有许多重叠的切片,当加起来时,会比它们的共享父级占用更多的内存),相当昂贵,并且会使垃圾收集器变慢.)

(For the record, we (V8 developers) know that this optimization can have negative effects on memory consumption, because it can cause long strings to be kept alive when just copying out the part that's still needed would use less memory. The problem is that in the general case, calculating whether that operation makes sense (because only a short chunk is still required) or not (because there are many overlapping slices which, when added up, would account for more memory than their shared parent), is fairly expensive and would make the garbage collector quite a bit slower.)

这篇关于Nodejs 分析:父级(切片字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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