Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

疑似内存泄露? #394

Closed
rojer95 opened this issue Jul 1, 2024 · 1 comment
Closed

疑似内存泄露? #394

rojer95 opened this issue Jul 1, 2024 · 1 comment

Comments

@rojer95
Copy link

rojer95 commented Jul 1, 2024

最近一个项目接入了pinyin这个库,提供了一个接口给前端调用,框架是基于nestjs的
接入后这个项目过一段时间就会OOM,后来把pinyin这个库去掉了,就正常了
也不知道是不是我使用的方法有问题

整体内存占用走势

image

使用pinyin库

刚刚启动

image

运行一段时间

image

去掉pinyin库后

刚刚启动

image

运行一段时间

image

相关代码

@Controller('/dict')
export class DictController {

  @Post('/pinyin')
  async getPinyin(@Body('str') str: string) {
    return { pinyin: getPinyin(str) };
  }
}
import { uniq } from 'lodash';
import pinyin from 'pinyin';

const replaceNumber = (i: string) => {
  let temp: string = i;
  const numbers = {
    1: '一',
    2: '二',
    3: '三',
    4: '四',
    5: '五',
    6: '六',
    7: '七',
    8: '八',
    9: '九',
  };

  Object.keys(numbers).map((number) => {
    temp = temp.replace(new RegExp(String(number), 'g'), numbers[number]);
  });

  return temp;
};

export const getPinyin = (str) => {
  try {
    let str1 = replaceNumber(str);
    if (str1 !== str) str1 = `${str} ${str1}`;
    const py = pinyin(str1, {
      heteronym: true,
      style: 'first_letter',
      compact: true,
      segment: true,
    });
    return uniq(
      py
        .map((i) => i.join(''))
        .join(' ')
        .toLowerCase()
        .split(/\s/)
        .filter((i) => !!i),
    ).join(' ');
  } catch (error) {
    return '';
  }
};
@rojer95 rojer95 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
@hotoo
Copy link
Owner

hotoo commented Jul 14, 2024

@rojer95 把 new RegExp 去掉,或者固化正则表达式不要每次 new。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants