I am into severe stability issues with the following code and I have no clue where to start looking.
I have 1G RAM, and 1G swap (RHEL 8) and start my jar with the following parameters:
/opt/jdk-13/bin/java -Xms128M -Xmx128M -jar /home/ec2-user/ai2.jar
I am experiencing OOM crashes, coredumps, sudden high-CPU (70.0), network socket errors after the program runs about 30 minutes. A "top" during this time does not indicate any kind of memory leak.
There is a folder with up to 30.000 files that contain a timestamp in their name. I want the program to read these filenames and determine if the timestamp is overdue. If so, the content of the file will be read to receive sending information and trigger an action.
fun main(agrs:Array<String>) {
// maybe this is not the most elegant way ???
while (true) {
qpath = "/home/ec2-user/queue_mb"
readFolderContent("mamba")
sleep(3000)
}
}
fun readFolderContent(appname:String) {
val files = File(qpath).list()
val count = files.count()
writeQueueCounter(appname,count)
files.forEach {
val ttlString = it.dropLast(8)
val ttl = ttlString.toInt()
val now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) // Millis
if (ttl < now) {
var jsonString = File("$qpath/$it").readText(Charsets.UTF_8)
val gson = GsonBuilder().create()
val qContent = gson.fromJson(jsonString, QueueContent::class.java)
send(qContent.from, qContent.to, appname)
File("$qpath/$it").delete()
}
}
}
fun writeQueueCounter(appname:String, value:Int) {
var now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
val jsonString = """
{
"queue":$value
}
"""
val client = OkHttpClient()
val url = URL("https://xxxxxx.firebaseio.com/$appname/current.json")
val mediaType = "application/json; charset=utf-8".toMediaType()
val body = jsonString.toRequestBody(mediaType)
val request = Request.Builder()
.url(url)
.addHeader("Content-Type", "application/json")
.patch(body)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
if (response.code == 200) {
println("Written $value Written To DB.$appname")
}
}
}