Skip to content

Commit d837337

Browse files
authoredNov 18, 2024··
Merge pull request #1235 from Thrameos/win_gc
Fix for GC on windows
2 parents 1a56a3b + c474ee8 commit d837337

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed
 

‎native/common/include/jp_gc.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,15 @@ class JPGarbageCollection
5656
bool java_triggered;
5757
PyObject *python_gc;
5858
jclass _SystemClass;
59+
jclass _ContextClass;
5960
jmethodID _gcMethodID;
6061

62+
jmethodID _totalMemoryID;
63+
jmethodID _freeMemoryID;
64+
jmethodID _maxMemoryID;
65+
jmethodID _usedMemoryID;
66+
jmethodID _heapMemoryID;
67+
6168
size_t last_python;
6269
size_t last_java;
6370
size_t low_water;
@@ -69,4 +76,4 @@ class JPGarbageCollection
6976
int python_triggered;
7077
} ;
7178

72-
#endif /* JP_GC_H */
79+
#endif /* JP_GC_H */

‎native/common/jp_gc.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ void JPGarbageCollection::init(JPJavaFrame& frame)
154154
_SystemClass = (jclass) frame.NewGlobalRef(frame.FindClass("java/lang/System"));
155155
_gcMethodID = frame.GetStaticMethodID(_SystemClass, "gc", "()V");
156156

157+
jclass ctxt = frame.getContext()->m_ContextClass.get();
158+
_ContextClass = ctxt;
159+
_totalMemoryID = frame.GetStaticMethodID(ctxt, "getTotalMemory", "()J");
160+
_freeMemoryID = frame.GetStaticMethodID(ctxt, "getFreeMemory", "()J");
161+
_maxMemoryID = frame.GetStaticMethodID(ctxt, "getMaxMemory", "()J");
162+
_usedMemoryID = frame.GetStaticMethodID(ctxt, "getUsedMemory", "()J");
163+
_heapMemoryID = frame.GetStaticMethodID(ctxt, "getHeapMemory", "()J");
164+
157165
running = true;
158166
high_water = getWorkingSize();
159167
limit = high_water + DELTA_LIMIT;
@@ -237,10 +245,24 @@ void JPGarbageCollection::onEnd()
237245
Py_ssize_t pred = current + 2 * (current - last);
238246
last = current;
239247
if ((Py_ssize_t) pred > (Py_ssize_t) limit)
248+
{
240249
run_gc = 2;
250+
limit = high_water + (high_water>>3) + 8 * (current - last);
251+
}
241252

242-
// printf("consider gc %d (%ld, %ld, %ld, %ld) %ld\n", run_gc,
243-
// current, low_water, high_water, limit, limit - pred);
253+
#if 0
254+
{
255+
JPJavaFrame frame = JPJavaFrame::outer(m_Context);
256+
jlong totalMemory = frame.CallStaticLongMethodA(_ContextClass, _totalMemoryID, nullptr);
257+
jlong freeMemory = frame.CallStaticLongMethodA(_ContextClass, _freeMemoryID, nullptr);
258+
jlong maxMemory = frame.CallStaticLongMethodA(_ContextClass, _maxMemoryID, nullptr);
259+
jlong usedMemory = frame.CallStaticLongMethodA(_ContextClass, _usedMemoryID, nullptr);
260+
jlong heapMemory = frame.CallStaticLongMethodA(_ContextClass, _heapMemoryID, nullptr);
261+
printf("consider gc run=%d (current=%ld, low=%ld, high=%ld, limit=%ld) %ld\n", run_gc,
262+
current, low_water, high_water, limit, limit - pred);
263+
printf(" java total=%ld free=%ld max=%ld used=%ld heap=%ld\n", totalMemory, freeMemory, maxMemory, usedMemory, heapMemory);
264+
}
265+
#endif
244266

245267
if (run_gc > 0)
246268
{

‎native/java/org/jpype/JPypeContext.java

+25
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,29 @@ private static void scanExistingJars()
637637
}
638638
}
639639

640+
private static long getTotalMemory()
641+
{
642+
return Runtime.getRuntime().totalMemory();
643+
}
644+
645+
private static long getFreeMemory()
646+
{
647+
return Runtime.getRuntime().freeMemory();
648+
}
649+
650+
private static long getMaxMemory()
651+
{
652+
return Runtime.getRuntime().maxMemory();
653+
}
654+
655+
private static long getUsedMemory()
656+
{
657+
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
658+
}
659+
660+
private static long getHeapMemory()
661+
{
662+
java.lang.management.MemoryMXBean memoryBean = java.lang.management.ManagementFactory.getMemoryMXBean();
663+
return memoryBean.getHeapMemoryUsage().getUsed();
664+
}
640665
}

0 commit comments

Comments
 (0)
Please sign in to comment.