记录复杂对象时发生内存泄漏 [英] Memory leak when logging complex objects

查看:84
本文介绍了记录复杂对象时发生内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个JavaScript库.在该库中,我想提供一些有关控制台内容的日志记录.

I am currently busy writting a javascript library. In that library I want to provide some logging about what is going to the console.

function log () {
        if ((window && typeof (window.console) === "undefined") || !enableLogging) {
            return false;
        }

        function currentTime() {
            var time = new Date();
            return time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds() + '.' + time.getMilliseconds();
        }

        var args = [];

        args.push(currentTime());

        for (var i = 1; i < arguments.length; i++) { 
            args.push(arguments[i]);
        }

        switch (arguments[0]) {
            case severity.exception:
                if (window.console.exception) {
                    window.console.exception.apply(console, args);
                } else {
                    window.console.error.apply(console, args);
                }
                break;
            case severity.error:
                window.console.error.apply(console, args);
                break;
            case severity.warning:
                window.console.warning.apply(console, args);
                break;
            case severity.information:
                window.console.log.apply(console, args);
                break;
            default:
                window.console.log.apply(console, args);
        }
        return true;
    }

上面的代码展示了我的日志功能.调用时,我至少提供安全性,消息和可选的一些对象(可以是DOM对象,例如IDBTransaction,IDBDatabase等).

The code above presents my log function. When called I provide at least a sevirity, a message and optionally some objects (can be DOM objects like IDBTransaction, IDBDatabase, ...).

log(severity.information, 'DB opened', db);

现在我遇到的问题是,这会导致内存泄漏.问题在于,我传递的对象通过console.log方法保留在内存中.有办法避免这种情况或清理吗?

Now the problem I have is that this results in a memory leak. The problem is that the objects that I pass stay in memory by the console.log method. Is there a way to avoid this or clean this up?

推荐答案

可以理解的是,传递给console.log的对象没有进行GC处理,因为在代码运行后,您需要能够在开发人员工具中对其进行检查.我根本不会将其称为问题,因为这就是调试应用程序时的方式,但是生产代码不应执行任何控制台日志记录.

It's understandable that objects passed to console.log aren't GC'ed because you need to be able to inspect them in developer tools after your code has run. I wouldn't call this a problem at all because that's how it needs to be when you are debugging your application but the production code shouldn't do any console logging.

如果您仍需要在生产中记录(或将其发送到某处),我建议

If you still need to log stuff (or send it somewhere) in production, I would suggest stringifying your object:

console.log(JSON.stringify(db, null, '\t'));  // '\t' to pretty print

这里的额外好处是,您在记录日志时确实捕获了对象的状态(在记录对象引用时保证 1 ).

The additional bonus here is that you really capture the object's state at the time of logging (while logging an object reference doesn't guarantee that 1).

由于您要记录大型对象,因此请注意,这样做可能会降低性能,因此,如果要在生产环境中进行操作,请三思.

Since you are trying to log large objects, be aware that doing this will probably decrease performance, so think twice if you want to do it in production.

1 -似乎已被修正 在最新的Chrome浏览器中

1 - seems to have been fixed in latest Chrome

这篇关于记录复杂对象时发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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