如何解决node.js“ JavaScript堆内存不足”的问题?

时间:2019-01-24 10:14:41

标签: node.js

我正在Ubuntu 16.04服务器上运行node.js服务。我最近遇到一个问题,收到以下错误消息:"UFATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory"

现在,我在SO上阅读到可以使用类似的命令:node --max-old-space-size=4096 yourFile.js来解决此问题,但是我并没有那样启动服务,而是使用supervisorctl start <servicename>来启动服务。 因此,我不确定如何解决我的问题。 你能建议我该怎么办?

1 个答案:

答案 0 :(得分:1)

supervisorctl 使用配置文件。它应该放在类似

的地方
bool ReadFileFromInternet(LPCWSTR WEB_URL, DWORD dwSize, LPSTR pszOutBuffer)
{
    DWORD dwDownloaded = 0;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(WEB_URL,
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect(hSession, WEB_URL,
            INTERNET_DEFAULT_HTTPS_PORT, 0);

    int err = GetLastError();

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = 
        WinHttpOpenRequest(hConnect, L"GET", NULL,
            NULL, WINHTTP_NO_REFERER,
            WINHTTP_DEFAULT_ACCEPT_TYPES,
            WINHTTP_FLAG_SECURE);
    err = GetLastError();

    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest(hRequest,
            WINHTTP_NO_ADDITIONAL_HEADERS, 0,
            WINHTTP_NO_REQUEST_DATA, 0,
            0, 0);
    err = GetLastError();

    // End the request.
    if (bResults)
    {
        err = GetLastError();
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }
    err = GetLastError();

    if (bResults)
    {
        if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
            dwSize, &dwDownloaded))
            printf("Error %u in WinHttpReadData.\n", GetLastError());
    }

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n", GetLastError());

    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);

    return bResults;
}

外观如下:

/etc/supervisor/conf.d/myapi.conf

在其中添加运行选项:

[program:my-api]
command=node /home/myuser/myapi/app.js
autostart=true
autorestart=true
environment=NODE_ENV=production
stderr_logfile=/var/log/myapi.err.log
stdout_logfile=/var/log/myapi.out.log
user=myuser