简体   繁体   中英

Java Heap Space: Out of Memory Error - Google App Engine

I am using App Engine Standard, with Java 8 language. Whenever I read a file from Cloud Storage and convert it into a String, I get an out-of-memory error. Here's the snippet:

    BlobId blobId = BlobId.of("<Bucket Name>", "<file_name>");  // file size is 19MB
    byte[] content = storage.readAllBytes(blobId);
    String contentString = new String(content, UTF_8); // this is causing the error

This is the log trace:

com.google.apphosting.runtime.jetty9.JettyLogger warn: Error for /transform (JettyLogger.java:29) java.lang.OutOfMemoryError: Java heap space 
    at java.lang.StringCoding.decode(StringCoding.java:215) 
    at java.lang.String.<init>(String.java:465) 
    at java.lang.String.<init>(String.java:517) 
    at com.google.apphosting.runtime.NullSandboxPlugin$LogStream.flush(NullSandboxPlugin.java:600) 
    at java.io.PrintStream.flush(PrintStream.java:338) 
    at com.google.apphosting.runtime.NullSandboxPlugin$LogPrintStream.print(NullSandboxPlugin.java:546) 
    at java.io.PrintStream.println(PrintStream.java:806) 
    at com.google.apphosting.runtime.NullSandboxPlugin$LogPrintStream.println(NullSandboxPlugin.java:468) 
    at com.gcds.app.controller.BaseController.doGet(BaseController.java:49) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1772) 
    at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759) 
    at com.google.apphosting.runtime.jetty9.SaveSessionFilter.doFilter(SaveSessionFilter.java:37) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759) 
    at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759) 
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:297) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)

Is there any way out of it?

I had the same issue. However, it can be solved by using in appengine-web.xml. I'm using AppEngine Java SDK. Depend on your , AppEngine will assign necessary instance for your application.

https://cloud.google.com/appengine/docs/standard/java/config/appref

One problem is that it can not be applied to the local development server. When I try to debug the current application, I experience the same error. In maven build environment, it's not easy to do that. If you use IDE such as Eclipse, there are pathway to avoid that. In my case, editing "Debug Configuration" with preferred memory size can resolve this issue.

Please find my "Debug Configuration" as following, [Program Argumennts]

--allow_remote_shutdown --disable_update_check --no_java_agent D:\workspace\xxxx

[VM Arguements]

-Xms2048m -Xmx8182m -Duse_jetty9_runtime=true  -D--enable_all_permissions=true   -Ddatastore.backing_store=D:\workspace\\local_db.bin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM