Hey guys , does anyone know how to limit the heap ...
# kobweb
d
Hey guys , does anyone know how to limit the heap size for your local kobweb server. I need it so i can test the following issue locally : I have written code that stores images as base64 (bsonbinary) in mongodb. At first I was just retrieving this base64 code and loading it on my webpage. On my local machine it's working but on my server I get a out of memory exception - java heap size. I use a free render server and it has 512mb memory. I already tried setting the following in my gradle properties but it only sets the available memory for starting server -- using gradle command
Copy code
org.gradle.jvmargs=-Xmx256m
Follow up question Does anyone know how to increase performance when passing these base64 images via json.encode(...) I'm currently trying to use gzip but it seems i can only use it in my jvmain folder and not my jsmain or commonmain
d
Does anyone know how to configure a ktor server to limit its heap?
I'm trying to find online articles for this and mostly failing
Also I wonder if the issue is ktor in your case or mongodb?
d
Uhmm at the moment i linked the same mongodb database to my local and server environment. So if it was mongodb it should also fail locally? πŸ€”
c
Copy code
org.gradle.jvmargs=-Xmx256m
This sets the memory given to Gradle itself, not the memory given to the started apps. I believe Kobweb uses the
application
plugin? (Not 100% sure, sorry). In that case, you can use:
Copy code
application {
    applicationDefaultJvmArgs = listOf("-Xmx256m")
}
Also, keep in mind that MongoDB cannot store a document larger than 16mb. Maybe that is your problem?
d
Yes indeed jvmargs is only used when setting up server aka using gradle to build
My images are only 2,1mb max. For images over 16mb you have to use Gridfs with mongodb.
My images are stored correctly the memory issue is when I get them from database and format them into json and return them with an api call πŸ™‚
Can't seem to use the application plugin 😞
s
can't you just use
Copy code
java -jar -Xmx256m file.jar
d
Not really familiar with setting these things on server. Where would you add this ?
s
I think you can just edit the run config here
d
Not really sure if it is the same as "kobweb run" but ill give it a try
It's probably the same as this file ? This didn't do anything for me πŸ˜›
s
very likely not the same, this sounds like it's vm options for the IDE, you want vm options for the process you're starting
d
image.png
only have this
s
you can try the kobwebStart gradle task and edit its config
c
You should be able to add
JAVA_OPTIONS="-Xmx256m"
in that "environement variables" selector. However, that will only impact running the app from IntelliJ. It won't do anything to your deployed app.
s
but ultimately it sounds like this issue concerns the underlying ktor layer rather than kobweb. so for debugging it's probably easiest to just create a standalone ktor repro
d
Doesn't seem to do anything even if i use very little memory
Yeah I was trying to avoid using a separate ktor. but it seems i have to
c
Is your repository public?
It's hard to say exactly why it doesn't work without seeing everything in action. If it's public, I'll try cloning it tonight to find it
It really shouldn't be too hard, it's just a matter of finding the correct place to put the config
d
No it's not public πŸ™‚
c
If you can publish a repository with exactly the same configuration as yours but without the actual code (except maybe a hello world), that should do the trick as well. I assume you started from one of the Kobweb examples?
d
Yes. I'll try to make a public clone in a few hours πŸ™‚
I found it!! I can use JAVA_TOOL_OPTIONS instead of JAVA_OPTIONS as environment variable in my run. Now I still have to find how to set it for project it self
πŸ‘ 1
And then it gives same error as on server
d
Yeah, for sure, a problem is going to be that Kobweb is running the ktor server for you.
But as I'm not sure what actually is causing your problem (mongodb growing in heap size, ktor growing in heap size, something else) we can spend a lot of time trying to give you suggestions for the wrong solution
You can definitely set environment variables on Render, and I think if you do that, it will be picked up by the ktor server that spins up
I'm not sure how to do it, but what I might try to do is figure out how to profile a server running locally
I don't know what OS you're on
But if you're going over 500MB on Render, then you're also going over 500MB on your local machine too
Here is the script that runs the ktor server:
Copy code
#!/bin/bash

# Find the kobweb project folder, using this file's location to start form
cd "$(dirname "$0")"
cd ../..

args="-Dkobweb.server.environment=PROD -Dkobweb.site.layout=KOBWEB -Dio.ktor.development=false -jar .kobweb/server/server.jar"

if [ -n "$KOBWEB_JAVA_HOME" ]; then
    "$KOBWEB_JAVA_HOME/bin/java" $args
elif [ -n "$JAVA_HOME" ]; then
    "$JAVA_HOME/bin/java" $args
else
    java $args
fi
which is very similar to what
kobweb run
does as well
It's possible I can add hooks for you somewhere to configure stuff that I don't already; but I don't know exactly what that is unfortunately
It's possible all you need to do is create a Kobweb Server Plugin but again I'm not sure, I'm sorry about that
d
I tested by changing xmx value locally and my code stops working below -xmx180m. So normally my render server has more than enough memory with 512?
d
I believe 512M is quite a lot for a simple web server. But not sure about one dealing with images.
d
I know πŸ˜›. But i needed to increase it on server. Im also going to improve the code managing the images. But for now i wanted to find difference between local and server
d
Sure! It's a good idea
d
JAVA_TOOL_OPTIONS was also solution on render btw
d
Have you looked at the Metrics tab for your failed server?
d
It works. But i'm still curious whats going to happen when i add more pictures
d
Oh, your render server is no longer crashing?
If you limit your heap, you're just going to get more GC collections running, which can slow down the site especially when it is under heavy load.
I doubt you're ever going to have to need 512MB of images in memory all the same time
c
Can you reproduce the problem locally? If so, the profiler should help quite a bit
d
Yeah i can produce it locally now πŸ˜› with java_tool_options. and thats what i set out to find πŸ˜›
Now I can improve my code and test quickly
d
Good luck!
d
Thanks for the help guys